用python爬取天气网长沙天气信息并保存到csv
时间: 2023-12-21 22:07:28 浏览: 47
好的,以下是实现这个功能的Python代码:
```python
import requests
import csv
# 设置请求头,伪装成浏览器访问
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 发送请求,获取长沙天气信息
url = 'http://www.weather.com.cn/weather/101250101.shtml'
response = requests.get(url, headers=headers)
response.encoding = 'utf-8'
html = response.text
# 解析HTML,获取长沙天气信息
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
weather_list = soup.select('.t ul li')
data_list = []
for weather in weather_list:
date = weather.select('.time')[0].text
detail_weather = weather.select('.wea')[0].text
temperature = weather.select('.tem')[0].text.replace('\n', '').strip()
wind_direction = weather.select('.win span')[0].text
wind_power = weather.select('.win span')[1].text
data_list.append([date, detail_weather, temperature, wind_direction, wind_power])
# 保存天气信息到csv文件中
with open('weather.csv', 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
writer.writerow(['日期', '天气', '温度', '风向', '风力'])
writer.writerows(data_list)
```
以上代码中,我们使用requests库发送请求,获取长沙天气信息的网页源代码。然后使用BeautifulSoup库解析HTML,从中获取长沙天气信息。最后使用csv库将天气信息保存到csv文件中。
可以根据需要修改代码中的url、headers、保存的文件名等参数。