python爬虫爬取天气网站:http://www.weather.com.cn/textFC/hb.shtml
时间: 2023-12-01 19:02:22 浏览: 111
爬取彼岸图网的壁纸 https://pic.netbian.com/
好的,以下是使用 Python 爬取天气网站的代码示例:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.weather.com.cn/textFC/hb.shtml'
response = requests.get(url)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.select_one('.conMidtab')
trs = table.select('tr')[1:]
for tr in trs:
tds = tr.select('td')
city = tds[0].text.strip()
weather = tds[1].text.strip()
temp = tds[2].text.strip()
print(city, weather, temp)
```
以上代码会输出该网站所有城市的天气信息,包括城市名、天气情况和温度。你可以根据需要进行修改和调整。
阅读全文