Skip to content

Instantly share code, notes, and snippets.

@zhkuo24
Last active August 29, 2015 14:18
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 zhkuo24/1f817d31b4675bad4be5 to your computer and use it in GitHub Desktop.
Save zhkuo24/1f817d31b4675bad4be5 to your computer and use it in GitHub Desktop.
获取天气预报信息
# -*- coding utf-8 -*-
# 获取天气预报信息
import urllib.request as request
import re
import pdb
def gethtml(url):
html = request.urlopen(url).read().decode()
return html
def getweather(html):
try:
weatherlist1 = re.compile(r'<a title="(.+?)天气预报"').findall(html)
weatherlist2 = re.compile(r'target="_blank"><img alt="(.+?)"').findall(html)
weatherlist3 = re.compile(r'target="_blank"><span>(.+?)</span></a>/<a ').findall(html)
weatherlist4 = re.compile(r'target="_blank"><b>(.+?)</b').findall(html)
#time=re.compile(r'span="4">(.+?)的预报<').findall(html)
time=re.compile(r'城市预报列表(.+?)<span>').findall(html)
pdb.set_trace()
print(time[0])
for i in (range(0,len(weatherlist1))):
print(weatherlist1[i],' ',weatherlist2[i],' ',weatherlist3[i],'/',weatherlist4[i])
except:
print('获取失败,请重试!')
url = "http://sc.weather.com.cn/index.shtml"
a = gethtml(url)
a = getweather(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment