Python Basic Combat-Crawler (Weather query system)

Source: Internet
Author: User

1 #requirements: Find weather in the region2 #Analysis: The first step, grab all the weather information above3 4  fromHtml.parserImportHtmlparser5  fromUrllibImportRequest6 ImportPickle7 ImportJSON8 9 #parsing Chinese weather net HTMLTen classWeatherhtmlparser (htmlparser): One     def __init__(self): ASelf.flag =False -Self.weather_data =None -Super (Weatherhtmlparser,self).__init__() the      -     defHandle_starttag (self,tag,attr): -         ifTag = ="Script": -Self.flag =True +  -     defHandle_endtag (self,tag): +         ifTag = ="Script": ASelf.flag =False at  -     defHandle_data (self,data): -         ifSelf.flag: -             if "var hour3data=" inchData: -data = Data.strip ("\ n") -data = Data.strip ("var hour3data=") inSelf.weather_data =json.loads (data) -                  to  + #National City weather Forecast code - classCitycodehtmlparser (htmlparser): the  *     def __init__(self): $Self.flag =FalsePanax NotoginsengSelf.city_dict = {} -Super (Citycodehtmlparser,self).__init__() the  +     defHandle_starttag (self,tag,attr): A         ifTag = ="P" orTag = ="BR": theSelf.flag =True +  -     defHandle_endtag (self,tag): $         ifTag = ="P" orTag = ="BR": $Self.flag =False -  -     defHandle_data (self,data): the         ifSelf.flag: -             if "=" inchData:Wuyidata = Data.split ("=") theSELF.CITY_DICT[DATA[1]] =Data[0] -              Wu                  - defPrintweatherinfo (func): About     defCall (): $info =func () -         ifinfo = =None: -             returnNone -  A         #the weather within a day +One_day = info["1d"] the          forIteminchOne_day: -Item = Item.split (",") $             Print("%s:: weather:%s; temp:%s; wind:%s; Wind:%s"% (item[0],item[2],item[3],item[4],item[5])) the  the         #weather in the next 7 days theFlag = input ("whether to print weather in the next 7 days:") the         ifFlag = ="is a": -Seven_day = info["7d"] in              forIinchRange (7): the                 ifI >= 1: the                      forIteminchSeven_day[i]: AboutItem = Item.split (",") the                         Print("%s:: weather:%s; temp:%s; wind:%s; Wind:%s"% (item[0],item[2],item[3],item[4],item[5])) the         Else: the             returnNone +  -     returnPager the Bayi  the  the #Fetching Weather Information - @printWeatherInfo - defgetallweather (): theCity = input ("Please enter the city you are inquiring about:") theCity =Querycitycode (city) the     ifCity = =None: the         returnNone -Url_address ="http://www.weather.com.cn/weather1d/%s.shtml"% City thereq =request. Request (url_address) theReq.add_header ("user-agent","mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/55.0.2883.87 safari/537.36") the With Request.urlopen (req) as HTML:94data = Html.read (). Decode ("Utf-8") theHtml_parser =Weatherhtmlparser () the html_parser.feed (data) the html_parser.close ()98         returnHtml_parser.weather_data About          - 101         102 #Query the code of the city103 defQuerycitycode (city_name):104  the     #crawl information from the Internet, originally I want to put in the file, blog garden can not pass, I slightly changed the next106     #At present the code is very ugly, first make a record, now after all no work, first the general knowledge over again107     defgetallcityinfo ():108Url_address ="http://doc.orz520.com/a/doc/2014/0322/2100581.html"109req =request. Request (url_address) theReq.add_header ("user-agent","mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/55.0.2883.87 safari/537.36")111 With Request.urlopen (req) as HTML: thedata = Html.read (). Decode ("Utf-8")113Html_parser =Citycodehtmlparser () the html_parser.feed (data) the html_parser.close () the             returnhtml_parser.city_dict117 118City_dict =Getallcityinfo ()119     ifCity_name not inchcity_dict: -         returnNone121     returnCity_dict[city_name]122         123         124 Getallweather () theAA = input ()

Python Basic Combat-Crawler (Weather query system)

Related Article

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.