...
|
...
|
@@ -2,45 +2,66 @@ from userio import *
|
2
|
2
|
import requests
|
3
|
3
|
import re
|
4
|
4
|
import newsParser
|
|
5
|
+from requests_html import HTML
|
|
6
|
+from requests_html import HTMLSession
|
|
7
|
+
|
5
|
8
|
|
6
|
9
|
def article(url):
|
7
|
10
|
say("Article: "+url)
|
8
|
|
- r = requests.get(url, allow_redirects=True, headers={'Accept-Encoding': 'deflate', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'})
|
9
|
|
- content = r.text
|
|
11
|
+ session = HTMLSession()
|
|
12
|
+ response = session.get(url,timeout=20)
|
|
13
|
+ pageContent=""
|
|
14
|
+ article_only=""
|
|
15
|
+ with response as r:
|
|
16
|
+ articleStrTitle = r.html.xpath('//meta[@property="og:title"]/@content')[0]
|
|
17
|
+ articleStrDescription = r.html.xpath('//meta[@property="og:description"]/@content')[0]
|
|
18
|
+ articleStrImageUrl = r.html.xpath('//meta[@property="og:image"]/@content')[0]
|
|
19
|
+ articleStrAuthor = r.html.xpath('//div[@class="author_wrapper"]/@content')
|
|
20
|
+ article=r.html.find("article")[0]
|
|
21
|
+ article_only+=article.html
|
|
22
|
+ lenBefore=len(article_only)
|
|
23
|
+ say("LengthBefore: "+str(lenBefore))
|
|
24
|
+ pageContent += "<meta property=\"og:type\" content=\"article\" />\n"
|
|
25
|
+ pageContent += "<meta property=\"og:title\" content=\""+articleStrTitle+"\" />\n"
|
|
26
|
+ pageContent += "<meta property=\"og:description\" content=\""+articleStrDescription+"\" />\n"
|
|
27
|
+ pageContent += "<meta property=\"og:url\" content=\""+url+"\" />\n"
|
|
28
|
+ pageContent += "<meta property=\"og:image\" content=\""+articleStrImageUrl+"\" />\n"
|
|
29
|
+ pageContent += "<meta property=\"og:image:type\" content=\"image/jpeg\" />\n"
|
|
30
|
+ #pageContent += "<meta name=\"author\" content=\""+articleStrAuthor+"\" />\n"
|
10
|
31
|
|
11
|
|
- articleStrImageUrl = newsParser.articleImage(content)
|
12
|
|
- articleStrTitle = newsParser.articleTitle(content)
|
13
|
|
- articleStrDescription = newsParser.articleDescription(content)
|
14
|
|
-
|
15
|
|
- pageContent = ""
|
16
|
|
- pageContent += "<meta property=\"og:type\" content=\"article\">\n"
|
17
|
|
- pageContent += "<meta property=\"og:title\" content=\""+articleStrTitle+"\">\n"
|
18
|
|
- pageContent += "<meta property=\"og:description\" content=\""+articleStrDescription+"\">\n"
|
19
|
|
- pageContent += "<meta property=\"og:url\" content=\""+url+"\">\n"
|
20
|
|
- pageContent += "<meta property=\"og:image\" content=\""+articleStrImageUrl+"\">\n"
|
21
|
|
- pageContent += "<meta property=\"og:image:type\" content=\"image/jpeg\">"
|
22
|
|
-
|
23
|
|
- articleCstBegin = "<article"
|
24
|
|
- articleCstEnd = "<div class=\"related-entries"
|
25
|
|
- articleCstEnd2 = "</article>"
|
26
|
|
- indexBegin = content.index(articleCstBegin)
|
27
|
|
- try:
|
28
|
|
- indexEnd = content.index(articleCstEnd)
|
29
|
|
- except:
|
30
|
|
- indexEnd = content.index(articleCstEnd2)
|
31
|
|
- article_only = content[indexBegin:indexEnd]
|
32
|
32
|
article_only = re.sub(r"<amp-img", '<img', article_only)
|
33
|
33
|
article_only = re.sub(r"</amp-img>", '', article_only)
|
34
|
34
|
article_only = re.sub(r"<h2", '<h3', article_only)
|
35
|
35
|
article_only = re.sub(r"</h2>", '</h3>', article_only)
|
36
|
36
|
article_only = re.sub(r"<h1", '<h2', article_only)
|
37
|
37
|
article_only = re.sub(r"</h1>", '</h2>', article_only)
|
38
|
|
- article_only = re.sub(r"<a class=\"share(.*?)\" data-social-name=\"(.*?)\" href=\"(.*?)\" target=\"_blank\">", '<a href="">', article_only)
|
39
|
|
- article_only = re.sub(r"<li class=\"(.*?) share-bar__item\">", '<li>', article_only)
|
40
|
|
- article_only = re.sub(r"<div class=\"share-bar share-bar--sticky yr-share\">",'<div class="share-bar share-bar--sticky yr-share" style="display:none;">', article_only)
|
41
|
|
- article_only = re.sub(r"<div class=\"(.*?) share-bar(.*?)>",'<div style="display:none;">', article_only)
|
42
|
|
- article_only = re.sub(r"<div class=\"yr-share\">",'<div style="display:none;">', article_only)
|
43
|
|
- article_only = article_only.replace("><", ">\n<")
|
44
|
|
-
|
45
|
|
- pageContent += "<article>"+article_only+"</article>"
|
|
38
|
+ article_only = re.sub(r'<script(.+?)</script>','',article_only,flags=re.M|re.S)
|
|
39
|
+ article_only = re.sub(r'<script(.+?)/>','',article_only)
|
|
40
|
+ article_only = re.sub(r'<form(.+?)</form>','',article_only,flags=re.M|re.S)
|
|
41
|
+ article_only = re.sub(r'<img src="(.+?)partage-(.+?).svg"/>','',article_only)
|
|
42
|
+ article_only = re.sub(r'<li data-socials=(.+?)</li>','',article_only,flags=re.M|re.S)
|
|
43
|
+ article_only = re.sub(r'<input type="email" (.+?)/>','',article_only)
|
|
44
|
+ article_only = re.sub(r'<link rel="stylesheet" type="text/css"(.+?)>','',article_only)
|
|
45
|
+ article_only = re.sub(r'<p class="asset asset-text">','<p>',article_only)
|
|
46
|
+ article_only = re.sub(r'<div class="homeNewsletter-notice">(.+?)</div>', '',article_only,flags=re.M|re.S)
|
|
47
|
+ article_only = re.sub(r'<button class="homeNewsletter-button">(.+?)</button>', '',article_only,flags=re.M|re.S)
|
|
48
|
+ article_only = re.sub(r'<div class="homeNewsletter-input">(.+?)</div>', '',article_only,flags=re.M|re.S)
|
|
49
|
+ article_only = re.sub(r'<div class="homeNewsletter-content">(.+?)</div>', '',article_only,flags=re.M|re.S)
|
|
50
|
+ article_only = re.sub(r'<div class="homeNewsletter-title">(.+?)</div>', '',article_only,flags=re.M|re.S)
|
|
51
|
+ article_only = re.sub(r'<div class="homeNewsletter-form">(.+?)</div>', '',article_only,flags=re.M|re.S)
|
|
52
|
+ #article_only = re.sub(r'','',article_only)
|
|
53
|
+ article_only = re.sub(r"href=\"/",'href="https://www.huffingtonpost.fr/',article_only)
|
|
54
|
+ article_only = re.sub(r"src=\"/",'src="https://www.huffingtonpost.fr/',article_only)
|
|
55
|
+ article_only = re.sub(r"^$",'',article_only)
|
|
56
|
+ article_only = re.sub(r'^\s*$', '',article_only,flags=re.M|re.S)
|
|
57
|
+ article_only = re.sub(r"><",'>\n<',article_only)
|
|
58
|
+ article_only = re.sub(r"> <",'>\n<',article_only)
|
|
59
|
+
|
|
60
|
+ #pageContent += "\n"+article_only+"\n"
|
|
61
|
+ pageContent += "<article>\n"+article_only+"\n</article>\n"
|
|
62
|
+ lenAfter=len(article_only)
|
|
63
|
+ lenGain=float(10000-int(float(100*lenAfter/lenBefore)*100))/100
|
|
64
|
+ say("LengthAfter : "+str(lenAfter))
|
|
65
|
+ say("Gain : "+str(lenGain)+"%")
|
46
|
66
|
return pageContent
|
|
67
|
+
|