Showing 1 changed files with 46 additions and 24 deletions
+46 -24
newsParser/newsParser/newsDNA.py
... ...
@@ -2,37 +2,59 @@ 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
-  url = url.replace("dna.fr/","dna.fr/amp/")
9
-  r = requests.get(url, allow_redirects=True)
10
-  content = r.text
11
-  
12
-  articleStrImageUrl = newsParser.articleImage(content)
13
-  articleStrTitle = newsParser.articleTitle(content)
14
-  articleStrDescription = newsParser.articleDescription(content)
15
-  
16
-  pageContent = ""
17
-  pageContent += "<meta property=\"og:type\" content=\"article\">\n"
18
-  pageContent += "<meta property=\"og:title\" content=\""+articleStrTitle+"\">\n"
19
-  pageContent += "<meta property=\"og:description\" content=\""+articleStrDescription+"\">\n"
20
-  pageContent += "<meta property=\"og:url\" content=\""+url+"\">\n"
21
-  pageContent += "<meta property=\"og:image\" content=\""+articleStrImageUrl+"\">\n"
22
-  pageContent += "<meta property=\"og:image:type\" content=\"image/jpeg\">"
23
-  
24
-  articleCstBegin = "<section poool-access-content amp-access=\"access\" amp-access-hide>"
25
-  articleCstEnd   = "<section amp-access=\"NOT error AND NOT access\" id=\"poool\">"
26
-  indexBegin = content.index(articleCstBegin)
27
-  indexEnd   = content.index(articleCstEnd)
28
-  article_only = content[indexBegin:indexEnd]
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"
31
+
29 32
   article_only = re.sub(r"<amp-img", '<img', article_only)
30 33
   article_only = re.sub(r"</amp-img>", '', article_only)
31 34
   article_only = re.sub(r"<h2", '<h3', article_only)
32 35
   article_only = re.sub(r"</h2>", '</h3>', article_only)
33 36
   article_only = re.sub(r"<h1", '<h2', article_only)
34 37
   article_only = re.sub(r"</h1>", '</h2>', article_only)
35
-  
36
-  article_only = re.sub(r"href=\"\/", 'href=\"//dna.fr/', article_only)
37
-  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'<svg(.+?)</svg>','',article_only,flags=re.M|re.S)
40
+  article_only = re.sub(r'<script(.+?)/>','',article_only)
41
+  article_only = re.sub(r'<ul class="GFWK_tagsList"(.+?)</ul>','',article_only,flags=re.M|re.S)
42
+  article_only = re.sub(r'<div id="sharingBlock"(.+?)</div>','',article_only,flags=re.M|re.S)
43
+  article_only = re.sub(r'<div id="PlugCommentsList2">(.+?)</div>','',article_only,flags=re.M|re.S)
44
+  article_only = re.sub(r'<button class="bt_neutral large hidden" id="loadMoreComments">COMMENTAIRES SUIVANTS</button>','',article_only)
45
+  article_only = re.sub(r'<button class="bt_default">OK</button>','',article_only)
46
+  #article_only = re.sub(r'','',article_only)
47
+  article_only = re.sub(r"href=\"/",'href="https://www.dna.fr/',article_only)
48
+  article_only = re.sub(r"src=\"/",'src="https://www.dna.fr/',article_only)
49
+  article_only = re.sub(r"^$",'',article_only)
50
+  article_only = re.sub(r'^\s*$', '',article_only,flags=re.M|re.S)
51
+  article_only = re.sub(r"><",'>\n<',article_only)
52
+
53
+  pageContent += "\n"+article_only+"\n"
54
+  #pageContent += "<article>\n"+article_only+"\n</article>\n"
55
+  lenAfter=len(article_only)
56
+  lenGain=float(10000-int(float(100*lenAfter/lenBefore)*100))/100
57
+  say("LengthAfter : "+str(lenAfter))
58
+  say("Gain        : "+str(lenGain)+"%")
38 59
   return pageContent
60
+