Using Python+selenium to obtain the PM2.5 data information and sort by air quality in North Guangzhou

Source: Internet
Author: User

from http://www.pm25.com/shenzhen.html crawl Beijing, Shenzhen, Shanghai, Guangzhou, Chengdu PM2.5 Index, and according to the air quality favorably to the difference sort, saved in txt document

#coding =UTF-8FROM&NBSP;SELENIUM&NBSP;IMPORT&NBSP;WEBDRIVERFROM&NBSP;TIME&NBSP;IMPORT&NBSP;SLEEPCLASS&NBSP;PM: Def __init__ (self): Self.dr = webdriver. Chrome () Self.pm25_info = self.get_pm25_info () Def get_pm25_info (self): city_list = [' Beijing ', ' Shenzhen ', ' Shanghai ', ' Guangzhou ', ' Chengdu ']pm_info_list = []i = 0while i  < len (city_list): Self.dr.get (' http://www.pm25.com/' +city_list[i]+ '. html ') sleep (3) city_name  = self.dr.find_element_by_css_selector ('. bi_loaction_city ') .text  #定位城市名字pm_exp  =  Self.dr.find_element_by_css_selector ('. Bi_aqiarea_num ') .text  #定位PM2.5 index air_quality =  Self.dr.find_element_by_css_selector ('. Bi_aqiarea_right span ') .text  #定位空气质量pm_info_list. Append (city _name, pm_exp, air_quality) I += 1pm_info_list.sort (Key=lambda x:float (x[1])) return  pm_info_listdef get_pm_file (self):self.file_title =  ' North Canton deep into five PM2.5 data information ' SELf.file = open (self.file_title +  '. txt ',  ' WB ') Self.floor = 1for item  in self.pm25_info:separate_line =  ' ********** '  +  ' TOP '  + str ( Self.floor)  +  ' **********\n ' Self.file.write (Separate_line.encode (' Utf-8 ')) Self.file.write (' City:   '  + item[0] +  ' \ n '   ' PM2.5 index:  '  + item[1] +  ' \ n '   ' Air quality: '  + item[2]+  ' \ n '). Encode (' Utf-8 ')) Self.floor += 1self.file.close () def quit (self): self.dr.quit () if __name__ ==  ' __main__ ':p m_info = pm () pm_info.get_pm_file () pm_ Info.quit ()

The page is as follows:

650) this.width=650; "alt=" Clipboard image.png "class=" selected "src=" https://attachments.tower.im/tower/ F6fa1944c7584be49b727ecb73bcd371?version=auto&filename=clipboard%20image.png "width=" 484 "height=" 594 "style= "Margin:.2em 0px;padding:0px;border:1px solid RGB (217,217,217); font-size:15px;vertical-align:middle;width:auto; Height:auto;color:rgb (51,51,51); font-family:arial, Sans-serif;white-space:normal;background-color:rgb ( 255,255,255); "/>

Generate TXT as follows:

650) this.width=650; "alt=" Clipboard image.png "class=" selected "src=" https://attachments.tower.im/tower/ B1bcc47d835c45d892d2ce25d14e6eaf?version=auto&filename=clipboard%20image.png "width=" 338 "height=" 354 "style= "Margin:.2em 0px;padding:0px;border:1px solid RGB (217,217,217); font-size:15px;vertical-align:middle;width:auto; Height:auto;color:rgb (51,51,51); font-family:arial, Sans-serif;white-space:normal;background-color:rgb ( 255,255,255); "/>

Using Python+selenium to obtain the PM2.5 data information and sort by air quality in North Guangzhou

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.