-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
60 lines (43 loc) · 1.81 KB
/
scraper.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import requests
from bs4 import BeautifulSoup
from user_agent import user_agent
headers = {'User-Agent': user_agent,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
def launch_request(url):
try:
resp = requests.get(url, headers=headers)
resp.raise_for_status()
except requests.exceptions.HTTPError as err:
raise SystemExit(err)
return resp
def get_main_news():
url = 'https://www.infomed.hlg.sld.cu/'
resp = launch_request(url)
content = BeautifulSoup(resp.text, 'lxml')
noticias = content.find_all('header', attrs={'class':'dmbs-post-header'})
titulares = []
for articulo in noticias:
titulares.append({
"titular": articulo.find('h2', attrs={'class':'dmbs-post-title'}).get_text(),
"url": articulo.find('h2').a.get('href')
})
return titulares
def get_info_by_news(noticia):
print(f'Scrapping {noticia["titular"]}')
resp = launch_request(noticia["url"])
content = BeautifulSoup(resp.text, 'lxml')
article = content.find('article', attrs={'class':'dmbs-post-single'})
noticia['fecha'] = article.find('span', attrs={'class':'dmbs-post-date'}).get_text()
noticia['articulo'] = article.find('div', attrs={'class':'card-body dmbs-post-content'}).get_text()
return noticia
if __name__ == '__main__':
noticias = get_main_news()
for noticia in noticias:
news = get_info_by_news(noticia)
print('=================================')
print(news)
print('=================================')