Showing 1 changed files with 56 additions and 1 deletions
+56 -1
newsParser/newsParser/newsBFM.py
... ...
@@ -2,10 +2,65 @@ 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 8
 def article(url):
7 9
   say("Article: "+url)
8
-  url = url.replace("dna.fr/","dna.fr/amp/")
10
+  session = HTMLSession()
11
+  response = session.get(url,timeout=20)
12
+  pageContent=""
13
+  article_only=""
14
+  with response as r:
15
+    articleStrTitle = r.html.xpath('//meta[@property="og:title"]/@content')[0]
16
+    articleStrDescription = r.html.xpath('//meta[@property="og:description"]/@content')[0]
17
+    articleStrImageUrl = r.html.xpath('//meta[@property="og:image"]/@content')[0]
18
+    articleStrAuthor = r.html.xpath('//div[@class="author_wrapper"]/@content')
19
+    print(articleStrAuthor)
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'<ul class="social_media" id="contain_social_media">(.+?)</ul>','',article_only)
39
+  article_only = re.sub(r'<script(.+?)</script>','',article_only)
40
+  article_only = re.sub(r'<source media=(.+?)/>','',article_only)
41
+  article_only = re.sub(r'<template(.+?)</template>','',article_only)
42
+  article_only = re.sub(r'<section class="block block_ordered_article">(.+?)</section>','',article_only)
43
+  article_only = re.sub(r'<section class="block_video_section"(.+?)</section>','',article_only)
44
+  article_only = re.sub(r'<nav role="breadcrumb"(.+?)</nav>','',article_only)
45
+  article_only = re.sub(r'<img width="(.+?)" height="(.+?)" src=','<img src=',article_only)
46
+  article_only = re.sub(r'<aside class="video_list_c">(.+?)</aside>','',article_only)
47
+  #article_only = re.sub(r'','',article_only)
48
+  article_only = re.sub(r"href=\"/",'href="https://bfmtv.com/',article_only)
49
+  article_only = re.sub(r"src=\"/",'src="https://bfmtv.com/',article_only)      
50
+  article_only = re.sub(r"^$",'',article_only)
51
+  article_only = re.sub(r'^\s*$', '',article_only,flags=re.M|re.S)
52
+  article_only = re.sub(r"><",'>\n<',article_only)
53
+  
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)+"%")
59
+  return pageContent
60
+
61
+
62
+def articleOld(url):
63
+  say("Article: "+url)
9 64
   r = requests.get(url, allow_redirects=True)
10 65
   content = r.text
11 66
   pageContent = ""