如何用python爬取空气质量_python爬取全国空气质量信息 具体代码 以及实现方法
时间: 2023-09-02 12:07:56 浏览: 162
zhp.zip_python爬取飞猪网机票_爬取飞猪_爬取飞猪网_爬猪网
5星 · 资源好评率100%
要爬取全国的空气质量信息,可以通过访问国家环保部网站(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中。
阅读全文