-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscraper.py
77 lines (55 loc) · 1.68 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import requests
from bs4 import BeautifulSoup
URL = 'https://www.elespectador.com/noticias/'
def run():
# --> Use of requests
try:
response = requests.get(URL)
except e:
print('Error:', e)
# Status code
# print(response.status_code)
# Headers
# print(response.headers)
# Encoding
# print(response.encoding)
# Body
# print(response.text)
# Request headers
# print(response.request.headers)
# Request methods
# print(response.request.method)
# --> Use of BeautifulSoup
# Creating a soup
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
print(type(soup))
# Extracting the date
today = soup.find('div', attrs={
'class': 'date'
}).get_text()
# print(today)
# Extracting the articles titles
titles_div = soup.find_all('div', attrs={
'class': 'node-title'
})
titles = [title_div.find('a').get_text() for title_div in titles_div]
# print(titles)
# OPTIONAL - Extracting the sections
sections_span = soup.find('div', attrs={
'class': 'menu-name-main-menu'
}).find('ul', attrs={
'class': 'menu'
}).find_all('span')
sections_titles = [span.get_text() for span in sections_span]
# print(sections_titles)
# Saving the articles titles
with open('result.txt', 'w', encoding='utf-8') as f:
for title in titles:
f.write(title)
f.write('\n')
else:
print('Error:', response.status_code)
exit()
if __name__ == '__main__':
run()