-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapes.py
70 lines (63 loc) · 2.75 KB
/
scrapes.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
from typing import Dict, Union, List
import requests
from dateutil import parser
from lxml import html
HTTP_HEADERS: Dict[str, str] = {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
}
def get_page_length() -> Union[int, None]:
"""Fetches the amount of available pages in the Lavoz news directory."""
response = requests.get("https://lavozdeanza.com/category/news",
headers=HTTP_HEADERS)
if response.status_code == 200:
tree = html.fromstring(response.content)
page_length: str = tree.cssselect(
'#fullhomepage div.navigation ol li:nth-child(10) a')[0].text
return int(page_length)
else:
raise ValueError(
'Something went wrong when fetching the page length information.')
def get_link_data(target: int) -> Union[List[Dict[str, str]], None]:
""" Retrieves links from a specific La Voz directory page."""
response = requests.get(
f"https://lavozdeanza.com/category/news/page/{target}/",
headers=HTTP_HEADERS)
if response.status_code == 200:
tree = html.fromstring(response.content)
links = tree.cssselect('#contentleft div div h2 a')
return [{"href": i.attrib['href'], "content": i.text} for i in links]
else:
raise ValueError(
'Something went wrong when fetching the link information.')
def get_story_data(url: str) -> Union[Dict[str, Union[str, List[str]]], None]:
"""Fetches Story Content from a La Voz story and returns a dictionary with it."""
response = requests.get(url, headers=HTTP_HEADERS)
if response.status_code == 200:
tree = html.fromstring(response.content)
headline = tree.cssselect('#contentleft div.postarea div h1')[0].text
by = tree.cssselect(
'#contentleft div.postarea div div.storydetails p span.storybyline'
)[0].text
date = tree.cssselect(
'#contentleft div.postarea div div.storydetails p span.storydate span'
)[0].text
tags = [
i.text for i in tree.cssselect(
'#contentleft div.postarea ul.snotags li a')
]
ps = tree.cssselect('#contentleft div.postarea div span div p');
ss = tree.cssselect(
'#contentleft div.postarea div span div p span')
content = [i.text for i in ps] if ps[0].text != None else [i.text for i in ss]
return {
"headline": headline,
"by": by,
"date": parser.parse(date),
"tags": tags,
"content": content,
"href": url
}
else:
raise ValueError(
'Something went wrong when fetching the story information.')