Skip to content

Instantly share code, notes, and snippets.

@sincerefly
Created November 4, 2016 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sincerefly/eb81bf505e718a0fde4a7e6d776347e7 to your computer and use it in GitHub Desktop.
Save sincerefly/eb81bf505e718a0fde4a7e6d776347e7 to your computer and use it in GitHub Desktop.
获取天气数据(2)
#!usr/bin/python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
import json
def get_html(url , data = None):
r = requests.get(url, timeout=10)
r.encoding = 'utf-8'
return r.text
def parser_data(html_text):
soup = BeautifulSoup(html_text, "html.parser")
day7 = soup.find('div', {'id': '7d'})
js = day7.find('script').text.encode('utf-8')
json_data = js.split('=')[1]
data = json.loads(json_data)
return data
if __name__ == '__main__':
url ='http://www.weather.com.cn/weather/101010100.shtml'
html = get_html(url)
result = parser_data(html)
for day in result['7d']:
print day[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment