Showing 1 changed files with 43 additions and 52 deletions
+43 -52
newsParser/newsParser/newsYahooCom.py
... ...
@@ -2,65 +2,56 @@ 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
-def articleTitle(content):
7
-  articleImgBegin ="<meta property=\"og:title\" content=\""
8
-  articleImgEnd   ="\" />"
9
-  indexImgBegin = content.index(articleImgBegin)
10
-  indexImgEnd   = content.index(articleImgEnd,indexImgBegin)
11
-  title = content[indexImgBegin+len(articleImgBegin):indexImgEnd]
12
-  return title
13
-  
14
-def articleImage(content):
15
-  articleImgBegin ="<meta property=\"og:image\" content=\""
16
-  articleImgEnd   ="\" />"
17
-  indexImgBegin = content.index(articleImgBegin)
18
-  indexImgEnd   = content.index(articleImgEnd,indexImgBegin)
19
-  title = content[indexImgBegin+len(articleImgBegin):indexImgEnd]
20
-  return title
21
-  
22
-  
23
-def articleDescription(content):
24
-  articleImgBegin ="<meta property=\"og:description\" content=\""
25
-  articleImgEnd   ="\" />"
26
-  indexImgBegin = content.index(articleImgBegin)
27
-  indexImgEnd   = content.index(articleImgEnd,indexImgBegin)
28
-  title = content[indexImgBegin+len(articleImgBegin):indexImgEnd]
29
-  return title
30
-  
31 9
 def article(url):
32 10
   say("Article: "+url)
33
-  url = url.replace("dna.fr/","dna.fr/amp/")
34
-  r = requests.get(url, allow_redirects=True)
35
-  content = r.text
36
-  articleCstBegin = "<div class=\"caas-body\">"
37
-  articleCstEnd   = "</article>"
38
-  articleStrTitle = articleTitle(content)
39
-  articleStrImageUrl = articleImage(content)
40
-  articleStrDescription = articleDescription(content)
41
-  
42
-  pageContent = ""
43
-  pageContent += "<meta property=\"og:type\" content=\"article\">\n"
44
-  pageContent += "<meta property=\"og:title\" content=\""+articleStrTitle+"\">\n"
45
-  pageContent += "<meta property=\"og:description\" content=\""+articleStrDescription+"\">\n"
46
-  pageContent += "<meta property=\"og:url\" content=\""+url+"\">\n"
47
-  pageContent += "<meta property=\"og:image\" content=\""+articleStrImageUrl+"\">\n"
48
-  pageContent += "<meta property=\"og:image:type\" content=\"image/jpeg\">"
49
-  
50
-  pageContent += "<h2>"+articleStrTitle+"</h2>\n"
51
-  pageContent += "<em>"+articleStrDescription+"</em>\n"
52
-  pageContent += "<img src=\""+articleStrImageUrl+"\">\n"
53
-  
54
-  indexBegin = content.index(articleCstBegin)
55
-  indexEnd   = content.index(articleCstEnd)
56
-  article_only = ""
57
-  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
+
58 32
   article_only = re.sub(r"<amp-img", '<img', article_only)
59 33
   article_only = re.sub(r"</amp-img>", '', article_only)
60 34
   article_only = re.sub(r"<h2", '<h3', article_only)
61 35
   article_only = re.sub(r"</h2>", '</h3>', article_only)
62 36
   article_only = re.sub(r"<h1", '<h2', article_only)
63 37
   article_only = re.sub(r"</h1>", '</h2>', article_only)
64
-  article_only = re.sub(r"href=\"\/", 'href=\"//news.yahoo.com/', article_only)
65
-  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'<div class="caas-share-buttons">(.+?)</div>','',article_only,flags=re.M|re.S)
41
+  article_only = re.sub(r'<button class="caas-button(.+?)</button>','',article_only,flags=re.M|re.S)
42
+  article_only = re.sub(r'<div class="caas-body-wrapper">','<img src="'+articleStrImageUrl+'"><div class="caas-body-wrapper">',article_only)
43
+  #article_only = re.sub(r'','',article_only)
44
+  article_only = re.sub(r"href=\"/",'href="https://xxxxx/',article_only)
45
+  article_only = re.sub(r"src=\"/",'src="https://xxxxx/',article_only)
46
+  article_only = re.sub(r"^$",'',article_only)
47
+  article_only = re.sub(r'^\s*$', '',article_only,flags=re.M|re.S)
48
+  article_only = re.sub(r"><",'>\n<',article_only)
49
+
50
+  #pageContent += "\n"+article_only+"\n"
51
+  pageContent += "<article>\n"+article_only+"\n</article>\n"
52
+  lenAfter=len(article_only)
53
+  lenGain=float(10000-int(float(100*lenAfter/lenBefore)*100))/100
54
+  say("LengthAfter : "+str(lenAfter))
55
+  say("Gain        : "+str(lenGain)+"%")
66 56
   return pageContent
57
+