Skip to content

Instantly share code, notes, and snippets.

@nick3499
Last active April 3, 2020 21:08
Show Gist options
  • Save nick3499/0af07f9a7aa28ed6216bb29d7be4b547 to your computer and use it in GitHub Desktop.
Save nick3499/0af07f9a7aa28ed6216bb29d7be4b547 to your computer and use it in GitHub Desktop.
Scrape Dark Sky Weather
#! /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)
@nick3499
Copy link
Author

nick3499 commented Apr 3, 2020

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:

_geo = '41.8369,-87.6847'  # Chicago
_geo = '34.1565,-118.5399'  # Los Angeles
_geo = '37.778,-122.4313'  # San Francisco
_geo = '40.7143,-74.006'  # New York

Test the raw code straight from this gist:

$ curl https://gist.githubusercontent.com/nick3499/0af07f9a7aa28ed6216bb29d7be4b547/raw/c6ecfe931e5b02fddf20714c5cf6c2f570c3d61e/dsky_scraper.py | python3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment