python爬取空气质量_Python爬取中国天气网天气

本文展示了如何使用Python爬虫从中国天气网获取特定城市的空气质量信息和详细天气预报,包括时间、状态、温度、风向、风力等数据,并提供了查看详细穿衣建议的功能。
摘要由CSDN通过智能技术生成

importsysimportreimportrequestsimportwebbrowserfrom PIL importImagefrom requests.exceptions importRequestExceptionimportcsv

data={}

with open("data.csv",‘r‘) as f:

rawinfos=list(csv.reader(f))for i inrawinfos:

data[i[0]]=i[1]defget_one_page(url,headers):try:

response=requests.get(url,headers=headers)if response.status_code==200:

response.encoding=‘utf-8‘

returnresponse.textreturnNoneexceptRequestException:returnNone

headers={‘User-Agent‘: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7‘}try:

address=data[sys.argv[1]]except:

sys.exit("\033[31m无该城市!\033[0m")

html=get_one_page(‘http://www.weather.com.cn/weather1d/‘+address+‘.shtml‘,headers)if nothtml:print("城市代码有误!")

exit(1)

ADDRES

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爬取空气质量检测的部分城市的历年每天质量数据 思路----------------------------------------- 从某城市的空气质量页获取某市每月的链接,再爬取每个月的表格数据。连云港市:https://www.aqistudy.cn/historydata/daydata.php?city=连云港 连云港2014年5月的空气质量:https://www.aqistudy.cn/historydata/daydata.php?city=连云港&month=2014-05 遇到的问题----------------------------------------- 获取的页中的表格数据隐藏,尝试requests无法获取。判断可能是动态加载的页 尝试----------------------------------------- 1. 通过XHR,js查找隐藏数据的加载页,没有找到。 2. 使用phantomjs.get() result=pd.read_html ,可以获得隐藏的表格数据,但是并不稳定,只是偶尔出现加载的表格数据,无法大规模的获取 解决方法----------------------------------------- 查找资料得知这个站的表格数据在Console里的items中, 使用selenium的webdriver.firefox(),driver.execute_script("return items") 数据可获得。 仍遇到的问题:----------------------------------------- 爬取一个页可获得数据,但是连续的获取页,会出现两个错误。 1.Message: ReferenceError: items is not defined 2.connection refused 解决方法: 1.connection refused问题,可能是页开太多,使用driver.quit() 2. 如果 execute_script 还是出错,可尝试pd.read_html获取信息。之前用phantomjs获取的时候输出空的表格,可能由于加载不够,用 Waite直到table出现之后再获取页 Element=wait.until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[3]/div[1]/div[1]/table/tbody"))) 3.之后出现偶尔出现输出为空,使用循环,如果输出表格为空,再重新获取。 if len(result)>1: filename = str(month) + '.xls' result.to_excel('E:\python\案例程序\data\\' + filename) print('成功存入'+filename) driver.quit() else: driver.quit() return getdata(monthhref,month)
爬取全国的空气质量信息,可以通过访问国家环保部站(http://www.mee.gov.cn/),找到空气质量相关的数据。 具体实现方法: 1. 首先,需要用requests模块向国家环保部站发起请求,获取相应的HTML页面。 ```python import requests url = "http://www.mee.gov.cn/ywgz/fgbz/bzwb/dqhjbh/dqhjzlbz/index.shtml" response = requests.get(url) html = response.content.decode("utf-8") ``` 2. 接下来,需要使用BeautifulSoup模块解析HTML页面,获取想要的数据。 ```python from bs4 import BeautifulSoup soup = BeautifulSoup(html, "html.parser") table = soup.find("table", {"class": "content"}) trs = table.find_all("tr") for tr in trs[1:]: tds = tr.find_all("td") city = tds[1].get_text() aqi = tds[2].get_text() print(city, aqi) ``` 3. 最后,将获取的数据存储到本地文件中。 ```python with open("aqi.txt", "w", encoding="utf-8") as f: for tr in trs[1:]: tds = tr.find_all("td") city = tds[1].get_text() aqi = tds[2].get_text() f.write(city + "\t" + aqi + "\n") ``` 完整代码如下: ```python import requests from bs4 import BeautifulSoup url = "http://www.mee.gov.cn/ywgz/fgbz/bzwb/dqhjbh/dqhjzlbz/index.shtml" response = requests.get(url) html = response.content.decode("utf-8") soup = BeautifulSoup(html, "html.parser") table = soup.find("table", {"class": "content"}) trs = table.find_all("tr") with open("aqi.txt", "w", encoding="utf-8") as f: for tr in trs[1:]: tds = tr.find_all("td") city = tds[1].get_text() aqi = tds[2].get_text() f.write(city + "\t" + aqi + "\n") ``` 这段代码可以爬取国家环保部站上的全国空气质量信息,并将其保存到本地文件aqi.txt中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值