Python capture weather

Source: Internet
Author: User

Python capture weather

The program is simple.

Find the data intercepted by the website based on the url.

#!/usr/bin/env pythonimport urllib2import reprovice=raw_input('input you provice')city=raw_input("input city:")url = "http://qq.ip138.com/weather/"+provice+"/"+city+".htm"#url = "http://qq.ip138.com/weather/anhui/HuoShan.htm"class Weather():        def __init__(self):                pass        def getHtml(self, url):                req = urllib2.Request(url)                res = urllib2.urlopen(req)                html = res.read()                res.close()                return html        def getWeather(self, html):                patterndate = re.compile(">(\d{4}-\d{1,2}-\d{1,2} .+)<")                date = patterndate.findall(html)                patternweather = re.compile("
(.+)") weather = patternweather.findall(html) patterntemperature = re.compile("([-]?\d{1,2}.+)") temperature = patterntemperature.findall(html) if len(date) == 0: print "Cannot get city's weather you input" return for i in range(len(date)): print "%s" % date[i], "\t%s" % weather[i], "\t%s" % temperature[i]if __name__ == "__main__": weather = Weather() weather.getWeather(weather.getHtml(url))

Prepare the database later

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.