Showing 1 changed files with 53 additions and 89 deletions
+53 -89
newsParser/newsParser/newsWaPo.py
... ...
@@ -2,105 +2,69 @@ 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
5 7
 
6
-def localArticleTitle(content):
7
-  articleElementBegin="<meta property=\"og:title\" content=\""
8
-  articleElementEnd  ="\"/>"
9
-  indexElementBegin  = content.index(articleElementBegin)
10
-  indexElementEnd    = content.index(articleElementEnd,indexElementBegin)
11
-  return content[indexElementBegin+len(articleElementBegin):indexElementEnd]
12
-
13
-def localArticleDescription(content):
14
-  articleElementBegin="<meta property=\"og:description\" content=\""
15
-  articleElementEnd  ="\"/>"
16
-  indexElementBegin  = content.index(articleElementBegin)
17
-  indexElementEnd    = content.index(articleElementEnd,indexElementBegin)
18
-  return content[indexElementBegin+len(articleElementBegin):indexElementEnd]
19
-  return ""
20
-
21
-def localArticleImage(content):
22
-  articleElementBegin="<meta property=\"og:image\" content=\""
23
-  articleElementEnd  ="\"/>"
24
-  indexElementBegin  = content.index(articleElementBegin)
25
-  indexElementEnd    = content.index(articleElementEnd,indexElementBegin)
26
-  return content[indexElementBegin+len(articleElementBegin):indexElementEnd]
27
-  return ""
28 8
 
29 9
 def article(url):
30 10
   say("Article: "+url)
31
-  r = requests.get(url, allow_redirects=True)
32
-  r.encoding = r.apparent_encoding
33
-  content = r.text
34
-  
35
-  articleStrImageUrl = localArticleImage(content)
36
-  articleStrTitle = localArticleTitle(content)
37
-  articleStrDescription = localArticleDescription(content)
38
-  articleStrImageUrl = re.sub(r"https://www\.washingtonpost\.com/wp-apps/imrs\.php\?src=(.+)&.+", r"\g<1>", articleStrImageUrl)
39
-  
40
-  pageContent = ""
41
-  pageContent += "<meta charset=\"utf-8\"/>"
42
-  pageContent += "<meta property=\"og:type\" content=\"article\">\n"
43
-  pageContent += "<meta property=\"og:title\" content=\""+articleStrTitle+"\">\n"
44
-  pageContent += "<meta property=\"og:description\" content=\""+articleStrDescription+"\">\n"
45
-  pageContent += "<meta property=\"og:url\" content=\""+url+"\">\n"
46
-  pageContent += "<meta property=\"og:image\" content=\""+articleStrImageUrl+"\">\n"
47
-  pageContent += "<meta property=\"og:image:type\" content=\"image/jpeg\">"
48
-  
49
-  articleCstBegin = "<article"
50
-  articleCstEnd   = "<div class=\"mt-md\">"
51
-  articleCstEnd2   = "</article>"
52
-  articleCstEnd3   = "<div class=\"flex mt-md\">"
53
-  indexBegin = content.index(articleCstBegin)
54
-  try:
55
-    indexEnd  = content.index(articleCstEnd)
56
-  except:
57
-    try:
58
-      indexEnd  = content.index(articleCstEnd2) 
59
-    except:
60
-      indexEnd = content.index(articleCstEnd3)
61
-  debug("indexBegin: "+str(indexBegin))
62
-  debug("indexEnd  : "+str(indexEnd))
63
-  say("Title: "+articleStrTitle)
64
-  say("Image: "+articleStrImageUrl)
65
-
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"
66 31
 
67
-  article_only = "<h2>"+articleStrTitle+"</h2>"
68
-  article_only += "<img src=\""+articleStrImageUrl+"\">"
69
-  article_only += "<em>"+articleStrDescription+"</em>"
70
-
71
-  with open("titi.html", "w") as f2:
72
-      f2.write(content[indexBegin:indexEnd])
73
-      f2.close
74
-  article_only += content[indexBegin:indexEnd]
75 32
   article_only = re.sub(r"<amp-img", '<img', article_only)
76 33
   article_only = re.sub(r"</amp-img>", '', article_only)
77 34
   article_only = re.sub(r"<h2", '<h3', article_only)
78 35
   article_only = re.sub(r"</h2>", '</h3>', article_only)
79 36
   article_only = re.sub(r"<h1", '<h2', article_only)
80 37
   article_only = re.sub(r"</h1>", '</h2>', article_only)
81
-  # ~ article_only = re.sub(r"<div data-sc-v=\"4.23.4\" data-sc-c=\"placeholder\">Advertisement</div>", '</h2>', article_only)
82
-  article_only = re.sub(r"<div data-sc-v=\"4\.24\.3\" data-sc-c=\"placeholder\">Advertisement</div>", '', article_only)
83
-  #article_only = re.sub(r"<div class=\"dib bg-white pl-xs pr-xs font-sans-serif light font-xxxxs lh-md gray-dark\" data-sc-v=\"4\.24\.3\" data-sc-c=\"adslot\">Story continues below advertisement</div>", '', article_only)
84
-  article_only = re.sub(r"<div class=\"dib flex divider.+?data-sc-c=\"adslot\">Story continues below advertisement</div>","", article_only)
85
-
86
-  article_only = re.sub(r"style=\"width:300px;height:250px\"", 'style=\"width:1px;height:1px\"', article_only)
87
-  article_only = re.sub(r"style=\"width:120px;height:32px\"", 'style=\"width:1px;height:1px\"', article_only)
88
-  article_only = re.sub(r"style=\"width:136px;height:20px\"", 'style=\"width:1px;height:1px\"', article_only)
89
-  article_only = re.sub(r"style=\"width:300px;height:600px\"", 'style=\"width:1px;height:1px\"', article_only)
90
-  article_only = re.sub(r"style=\"min-height:250px\"", 'style=\"min-height:1px\"', article_only)
91
-  article_only = re.sub(r"style=\"min-height:298px\"", 'style=\"min-height:1px\"', article_only)
92
-  article_only = re.sub(r"style=\"min-height:600px\"", 'style=\"min-height:1px\"', article_only)
93
-  article_only = re.sub(r"class=\"center absolute w-100\" style=\"top:-12px\"", '', article_only)
94
-  article_only = re.sub(r"class=\"center absolute w-100\" style=\"top:-12px\"", '', article_only)
95
-  article_only = re.sub(r"<div data-qa=\"drop-cap-letter\">", '<div>', article_only)
96
-  article_only = re.sub(r"filter:blur\(10px\);", '', article_only)
97
-  article_only = re.sub(r"<div class=\"bg-pattern-1\".+?>", '<div>', article_only)
98
-  article_only = re.sub(r"<div class=\"bg-pattern-2\".+?>", '<div>', article_only)
99
-  article_only = re.sub(r"<img class=\"dn canvas-foreground\" src=\".+?\"/>", '', article_only)
100
-  article_only = re.sub(r"<div class=\"subhead .+?>", '<div>', article_only)
101
-  #article_only = re.sub(r"<canvas id=\"artboard\" style=\".+\">", '<canvas>', article_only)
102
-  #article_only = re.sub(r"", '', article_only)
103
-  article_only = article_only.replace("><", ">\n<")
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'<button class="wpds(.+?)</button>','',article_only,flags=re.M|re.S)
41
+  article_only = re.sub(r'<div>Advertisement</div>','',article_only)
42
+  article_only = re.sub(r'<div class="flex flex-column(.+?)</div>','',article_only,flags=re.M|re.S)
43
+  article_only = re.sub(r'<div data-testid="placeholder-box" class="w-100 h-100 absolute flex flex-column justify-center border-box bg-offwhite" style="width:300px;height:250px">','<div>',article_only)
44
+  article_only = re.sub(r'<div aria-hidden="true" class="hide-for-print relative flex justify-center content-box items-center b bh mb-md mt-sm pt-sm pb-sm" style="min-height:250px;border-top-color:;border-bottom-color:">','<div>',article_only)
45
+  article_only = re.sub(r'<div aria-hidden="true" class="hide-for-print relative flex justify-center content-box items-center b bh mb-md mt-none pt-lg pb-lg" style="min-height:250px;border-top-color:;border-bottom-color:">','<div>',article_only)
46
+  article_only = re.sub(r'<button class="inline-flex(.+?)</button>','',article_only,flags=re.M|re.S)
47
+  article_only = re.sub(r'<div class="cb dn db-ns" data-qa="article-body-ad">','<div>',article_only)
48
+  article_only = re.sub(r'<svg (.+?)</svg>','',article_only,flags=re.M|re.S)
49
+  article_only = re.sub(r'<div class="center absolute w-100 border-box" style="top:">','<div>',article_only)
50
+  article_only = re.sub(r'<p data-qa="drop-cap-letter" data-el="text" class="font-copy font--article-body gray-darkest ma-0 pb-md">','<p>',article_only)
51
+  article_only = re.sub(r'<div class="article-body" data-qa="article-body">','<div>',article_only)
52
+  article_only = re.sub(r'<div class="cb db dn-ns" data-qa="article-body-ad">','<div>',article_only)
53
+  #article_only = re.sub(r'<img style="(.+?)" (.+?)>','<img src="'+articleStrImageUrl+'">',article_only)
54
+  article_only = re.sub(r'<div style="filter:blur(.+?)" class="w-100 mw-100 h-auto" width="600" height="(.+?)">','<div>',article_only)
55
+  article_only = re.sub(r'<div style="min-height:358px"/>','<div>',article_only)
56
+  #article_only = re.sub(r'','',article_only)
57
+  article_only = re.sub(r"href=\"/",'href="https://xxxxx/',article_only)
58
+  article_only = re.sub(r"src=\"/",'src="https://xxxxx/',article_only)
59
+  article_only = re.sub(r"^$",'',article_only)
60
+  article_only = re.sub(r'^\s*$', '',article_only,flags=re.M|re.S)
61
+  article_only = re.sub(r"><",'>\n<',article_only)
104 62
 
105
-  pageContent += "<article>"+article_only+"</article>"
63
+  #pageContent += "\n"+article_only+"\n"
64
+  pageContent += "<article>\n"+article_only+"\n</article>\n"
65
+  lenAfter=len(article_only)
66
+  lenGain=float(10000-int(float(100*lenAfter/lenBefore)*100))/100
67
+  say("LengthAfter : "+str(lenAfter))
68
+  say("Gain        : "+str(lenGain)+"%")
106 69
   return pageContent
70
+