-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherForecast.py
71 lines (59 loc) · 1.75 KB
/
weatherForecast.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
'''
Web scraping national weather forecasts
San Francisco
@author: Adams-Developer
'''
import requests
import itertools
import numpy as np
from bs4 import BeautifulSoup
import pandas as pd
page = requests.get("http://forecast.weather.gov/MapClick.php?lat=37.7772&lon=-122.4168")
soup = BeautifulSoup(page.content, 'html.parser')
seven_day = soup.find(id="seven-day-forecast")
forecast_items = seven_day.find_all(class_="tombstone-container")
tonight = forecast_items[0]
print(tonight.prettify())
print("\n")
#Extracting information from page
#Forecast period - Tonight
#Short description of weather conditions
#The low temperature
period = tonight.find(class_="period-name").get_text()
short_desc = tonight.find(class_="short-desc").get_text()
temp = tonight.find(class_="temp").get_text()
print(period)
print(short_desc)
print(temp)
print("\n")
#Extract the title attribute from img tag
#Treat BeautifulShop obj like dictionary
#and pass in the attribute we want to use as a key
img = tonight.find("img")
desc = img['title']
print(desc)
print("\n")
#Extract everything
#Select all items with the class period-name
#inside an item with the class tombstone-container
#call get_text on each BeautifulSoup obj
period_tags = seven_day.select(".tombstone-container .period-name")
periods = [pt.get_text() for pt in period_tags]
periods
['Tonight',
'Thursday',
'ThursdayNight',
'Friday',
'FridayNight',
'Saturday',
'SaturdayNight',
'Sunday',
'SundayNight']
#getting the other fields
short_descs = [sd.get_text() for sd in seven_day.select(".tombstone-container .short-desc")]
temps = [t.get_text() for t in seven_day.select(".tombstone-container .temp")]
descs = [d["title"] for d in seven_day.select(".tombstone-container img")]
print(short_descs)
print(temps)
print(descs)
print("\n")