# -*- Coding: utf-8 -*-# author:baoshanimport requestsfrom bs4 import BeautifulSoupdef get_city_aqi(city_pinyin): url = ‘http://pm25.in/‘+city_pinyin r = requests.get(url,timeout=30) soup = BeautifulSoup(r.text,‘lxml‘) div_List = soup.find_all(‘div‘,{"class":"span1"}) city_aqi = [] for i in range(8): div_content = div_List[i] caption = div_content.find(‘div‘,{"class": "caption"}).text.strip() value = div_content.find(‘div‘,{"class":"value"}).text.strip() city_aqi.append((caption,value)) return city_aqidef get_all_citIEs(): url = ‘http://pm25.in/‘ city_List = [] r = requests.get(url,‘lxml‘) city_div = soup.find_all(‘div‘,{‘class‘:‘bottom‘})[1] city_link_List = city_div.find_all(‘a‘) for city_link in city_link_List: city_name = city_link.text city_pinyin = city_link[‘href‘][1:] city_List.append((city_name,city_pinyin)) return city_Listdef main(): city_List = get_all_citIEs() for city in city_List: city_name = city[0] city_pinyin = city[1] city_aqi = get_city_aqi(city_pinyin) print(city_name,city_aqi)if __name__ == ‘__main__‘: main()
参考自:小象学院的AQI空气质量指数计算
总结以上是内存溢出为你收集整理的Python爬取所有城市的aqi值全部内容,希望文章能够帮你解决Python爬取所有城市的aqi值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)