“”""
作者:小锋
功能描述:网络爬虫
版本号:2.0
“”"
根据小象学习案例做的小测试,只是将结果打印在控制台,还没有转字符串类型,想保存在本地的可以自己添加
import requests
from bs4 import BeautifulSoup
def get_city_aqi(city_pinyin):
“”""
返回城市AQI
“”"
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=[]
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.append((caption,value))
return city
def get_city_name():
url = ‘http