#Python爬虫天气预览总结
![](https://img-blog.csdnimg.cn/20201217091302382.png)
一、爬取天气预报思路
- 确定爬取的天气预报城市网址
首先确定所要爬取天气预报的地址,找到各个城市查询的网址,
例如:https://www.tianqi.com/chinacity.html
url = 'https://www.tianqi.com/chinacity.html'
2.模拟浏览器发出请求
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
response = requests.get(url, headers=headers).content.decode('utf-8')
data = etree.HTML(response)
3.解析数据
在所选城市页面需要先找到每一个城市的天气预报地址,在通过地址进入所需页面获取天气预报信息。即需要两次获得网址在解析。
第一次获取城市的网址:
name_data = []
for text_data in weather_list:
city_data = ''.join(text_data)
name_data.append(city_data)
name_list = []
list_data