Showing 1 changed files with 29 additions and 7 deletions
+29 -7
newsParser/newsParser/newsWaPo.py
... ...
@@ -10,15 +10,35 @@ def article(url):
10 10
   say("Article: "+url)
11 11
   session = HTMLSession()
12 12
   response = session.get(url,timeout=20)
13
+  say("Return code: "+str(response.status_code))
13 14
   pageContent=""
14 15
   article_only=""
15 16
   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
17
+    articleStrTitle = ""
18
+    articleStrDescription = ""
19
+    articleStrImageUrl = ""
20
+    articleStrAuthor = ""
21
+    try:
22
+      articleStrTitle = r.html.xpath('//meta[@property="og:title"]/@content')[0]
23
+      articleStrDescription = r.html.xpath('//meta[@property="og:description"]/@content')[0]
24
+      articleStrImageUrl = r.html.xpath('//meta[@property="og:image"]/@content')[0]
25
+      articleStrAuthor = r.html.xpath('//div[@class="author_wrapper"]/@content')
26
+    except:
27
+      error("Extracting Metadatas")
28
+      article_only+="<p>ERROR: Extracting Metadatas<p>"
29
+    try:
30
+      article=r.html.find("article")[0]
31
+      article_only+=article.html
32
+    except:
33
+      #article_only+="<p>WARN: Tag not found<p>"
34
+      #article_only+="<p><a href=\""+url+"\">"+url+"</a></p>"
35
+      article_only+="<h1>"+articleStrTitle+"</h1>\n"
36
+      article_only+="<em>"+articleStrDescription+"</em>\n"
37
+      article_only+="<img src=\""+articleStrImageUrl+"\">\n"
38
+      for div in r.html.find('div'):
39
+        if "class" in div.attrs:
40
+          if "article-body" in div.attrs['class']:
41
+            article_only+=div.html+"\n"
22 42
     lenBefore=len(article_only)
23 43
     say("LengthBefore: "+str(lenBefore))
24 44
     pageContent += "<meta property=\"og:type\" content=\"article\" />\n"
... ...
@@ -63,7 +83,9 @@ def article(url):
63 83
   #pageContent += "\n"+article_only+"\n"
64 84
   pageContent += "<article>\n"+article_only+"\n</article>\n"
65 85
   lenAfter=len(article_only)
66
-  lenGain=float(10000-int(float(100*lenAfter/lenBefore)*100))/100
86
+  lenGain=0
87
+  if lenBefore != 0:
88
+    lenGain=float(10000-int(float(100*lenAfter/lenBefore)*100))/100
67 89
   say("LengthAfter : "+str(lenAfter))
68 90
   say("Gain        : "+str(lenGain)+"%")
69 91
   return pageContent