Last active
April 3, 2020 21:08
-
-
Save nick3499/0af07f9a7aa28ed6216bb29d7be4b547 to your computer and use it in GitHub Desktop.
Scrape Dark Sky Weather
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/python3 | |
'''After Dark Sky joined Apple, free API subscriptions closed. Enter the web \ | |
scraper.''' | |
from lxml import html | |
from requests import get | |
_geo = '37.778,-122.4313' # San Francisco | |
_page = get(f'https://darksky.net/forecast/{_geo}/us12/en') | |
tree = html.fromstring(_page.content) | |
# temperature | |
summary_swap = tree.xpath('//span[@class="summary swap"]/text()') | |
feels_like = tree.xpath('//span[@class="feels-like-text"]/text()') | |
low_temp = tree.xpath('//span[@class="low-temp-text"]/text()') | |
high_temp = tree.xpath('//span[@class="high-temp-text"]/text()') | |
extended_summary = tree.xpath('//span[@class="currently__summary next swap"]/text()') | |
# wind | |
wind_speed_value = tree.xpath('//span[@class="num swip wind__speed__value"]/text()') | |
wind_direction = tree.xpath('//span[@class="direction"]/@title') | |
# humidity | |
humidity = tree.xpath('//span[@class="num swip humidity__value"]/text()') | |
dew_point = tree.xpath('//span[@class="num dew__point__value"]/text()') | |
# UV, visibility | |
uv_index = tree.xpath('//span[@class="num uv__index__value"]/text()') | |
visibility = tree.xpath('//span[@class="num swip visibility__value"]/text()') | |
# pressure | |
pressure = tree.xpath('//span[@class="num swip pressure__value"]/text()') | |
# week summary | |
week_summary = tree.xpath('//div[@id="week"]/div[@class="summary"]/text()') | |
observations = [ | |
f'Summary: {summary_swap[0]:>9}', | |
f'Extended: {extended_summary[0]}', | |
f'Feels like: {feels_like[0]:>1}℉', | |
f'Low: {low_temp[0]:>10}℉', | |
f'High: {high_temp[0]:>9}℉', | |
f'Wind: {wind_direction[0]:>8} @ {wind_speed_value[0]}mph', | |
f'Humidity: {humidity[0]:>4}%', | |
f'Dew point: {dew_point[0]:>3}℉', | |
f'UV index: {uv_index[0]:>3}', | |
f'Visibility: {visibility[0]}mi', | |
f'Pressure: {pressure[0]:>6}mb', | |
f'Week Summary: {week_summary[0]:>6}'] | |
for ob in observations: | |
print(ob) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Enter syntax for location into Dark Sky's search engine to get geo coordinates from them because any old coordinates will default to New York. The following coordinates come from Dark Sky:
Test the raw code straight from this gist: