Debian Codegraph @

resources/scripts/scrapers/wiki-inthenews.py

raw symbols SCIP
1 # Obtains Wikipedia's "In the news" today's articles.
2
3 import urllib.request
4 import re
5 import json
6 from newspaper import Article
7
8 url = "https://en.wikipedia.org/wiki/Main_Page"
9 response = urllib.request.urlopen(url)
10 text = response.read().decode("utf-8")
11
12 text_li = re.search("In the news[\S\n\t\v ]+?<ul>([\S\n\t\v ]+?)<\/ul>", text).group(1)
13 articles_li = re.findall("<li>([\S\n\t\v ]+?)<\/li>", text_li)
14
15 # Iterate all articles and generate JSON feed entries.
16 wiki_base_url = "https://en.wikipedia.org"
17
18
19 json_feed = "{{\"title\": \"Wikipedia - In the news\", \"items\": [{items}]}}"
20 items = list()
21
22 for article in articles_li:
23 article_url = wiki_base_url + re.search("^.+?href=\"(.+?)\"", article).group(1)
24
25 f = Article(article_url, keep_article_html = True)
26 f.download()
27 f.parse()
28
29 article_url = json.dumps(article_url)
30 article_title = json.dumps(f.title)
31 article_html = json.dumps(f.article_html)
32 items.append("{{\"title\": {title}, \"content_html\": {html}, \"url\": {url}}}".format(title=article_title,
33 html=article_html,
34 url=article_url))
35
36 json_feed = json_feed.format(items=", ".join(items))
37
38 print(json_feed)