-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemperature.py
More file actions
49 lines (38 loc) · 1.71 KB
/
temperature.py
File metadata and controls
49 lines (38 loc) · 1.71 KB
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
from selectorlib import Extractor
import requests
class Temperature:
"""A scraper that uses an yml file to read the xpath of a value it needs to extract
from the timeanddate.com/weather/ url"""
headers = {
'pragma': 'no-cache',
'cache-control': 'no-cache',
'dnt': '1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
}
base_url = 'https://www.timeanddate.com/weather/'
yml_path = 'temperature.yaml'
def __init__(self, country, city):
self.country = country.replace(" ", "-")
self.city = city.replace(" ", "-")
def _build_url(self):
"""Builds the url string adding country and city"""
url = self.base_url + self.country + "/" + self.city
return url
def _scrape(self):
"""Extracts a value as instructed by the yml file and returns a dictionary"""
url = self._build_url()
extractor = Extractor.from_yaml_file(self.yml_path)
r = requests.get(url, headers=self.headers)
full_content = r.text
raw_content = extractor.extract(full_content)
return raw_content
def get(self):
"""Cleans the output of _scrape"""
scraped_content = self._scrape()
return float(scraped_content['temp'].replace("°C", "").strip())
if __name__ == "__main__":
temperature = Temperature(country="usa", city="san francisco")
print(temperature.get())