Showing 1 changed files with 52 additions and 0 deletions
+52
newsParser/newsParser/newsXXX.skel
... ...
@@ -0,0 +1,52 @@
1
+from userio import *
2
+import requests
3
+import re
4
+import newsParser
5
+from requests_html import HTML
6
+from requests_html import HTMLSession
7
+
8
+
9
+def article(url):
10
+  say("Article: "+url)
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("main")[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
+
32
+  article_only = re.sub(r"<amp-img", '<img', article_only)
33
+  article_only = re.sub(r"</amp-img>", '', article_only)
34
+  article_only = re.sub(r"<h2", '<h3', article_only)
35
+  article_only = re.sub(r"</h2>", '</h3>', article_only)
36
+  article_only = re.sub(r"<h1", '<h2', article_only)
37
+  article_only = re.sub(r"</h1>", '</h2>', article_only)
38
+  article_only = re.sub(r'<script(.+?)</script>','',article_only)
39
+  #article_only = re.sub(r'','',article_only)
40
+  article_only = re.sub(r"href=\"/",'href="https://xxxxx/',article_only)
41
+  article_only = re.sub(r"src=\"/",'src="https://xxxxx/',article_only)
42
+  article_only = re.sub(r"^$",'',article_only)
43
+  article_only = re.sub(r'^\s*$', '',article_only,flags=re.M|re.S)
44
+  article_only = re.sub(r"><",'>\n<',article_only)
45
+
46
+  pageContent += "<article>\n"+article_only+"\n</article>\n"
47
+  lenAfter=len(article_only)
48
+  lenGain=float(10000-int(float(100*lenAfter/lenBefore)*100))/100
49
+  say("LengthAfter : "+str(lenAfter))
50
+  say("Gain        : "+str(lenGain)+"%")
51
+  return pageContent
52
+