-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkommersant_parser.py
34 lines (29 loc) · 991 Bytes
/
kommersant_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from bs4 import BeautifulSoup as web
from basic_parser import Parser
class Kommersant_parser(Parser):
def __init__(self):
super().__init__("http://www.kommersant.ru/RSS/news.xml")
self.encoding = "windows-1251"
self.site_encoding = "windows-1251"
def grab(self, url):
content = web(self.get_content(url, self.site_encoding), "html.parser")
obj = {}
if content.select(".article_name"):
obj["title"] = content.select(".article_name")[0].text
if content.select(".article_text_wrapper > p"):
obj["content"] = list(
map(
lambda x: x.text,
content.select(".article_text_wrapper > p")
)
)
return obj
if __name__ == "__main__":
kom = Kommersant_parser()
news = kom.news()
print(news)
url = news[0]["link"]
title = news[0]["title"]
print(title, url)
data = kom.grab(url)
print(data)