Created
November 4, 2016 04:49
-
-
Save sincerefly/eb81bf505e718a0fde4a7e6d776347e7 to your computer and use it in GitHub Desktop.
获取天气数据(2)
This file contains 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
#!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