帮我写一个用python爬取上海过去10年的天气数据并保存到tianqi.csv文件中的代码
时间: 2024-05-04 18:15:36 浏览: 109
爬取指定城市气象数据python代码
好的,以下是代码:
```python
import requests
from bs4 import BeautifulSoup
import csv
url = "http://www.tianqihoubao.com/lishi/shanghai/month/"
# 创建CSV文件并写入表头
with open("tianqi.csv", "w", newline="", encoding="utf-8-sig") as f:
writer = csv.writer(f)
writer.writerow(["日期", "最高气温", "最低气温", "天气", "风向"])
# 爬取过去10年的天气数据
for year in range(2011, 2021):
for month in range(1, 13):
if month < 10:
month = "0" + str(month)
else:
month = str(month)
page_url = url + str(year) + month + ".html"
response = requests.get(page_url)
response.encoding = "gbk"
soup = BeautifulSoup(response.text, "html.parser")
tr_list = soup.find_all("tr")[1:]
for tr in tr_list:
td_list = tr.find_all("td")
date = td_list[0].text
temp_high = td_list[1].text
temp_low = td_list[2].text
weather = td_list[3].text
wind = td_list[4].text
# 将数据写入CSV文件
with open("tianqi.csv", "a", newline="", encoding="utf-8-sig") as f:
writer = csv.writer(f)
writer.writerow([date, temp_high, temp_low, weather, wind])
```
希望能帮到你!
阅读全文