利用爬虫程序爬取天气数据源
时间: 2023-07-12 08:55:03 浏览: 115
好的,以下是一个简单的Python程序,使用爬虫爬取天气数据源的方法:
```python
import requests
from bs4 import BeautifulSoup
def get_weather(city):
url = f"https://www.tianqi.com/{city}/"
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'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
weather = soup.select('.wea')[0].text.strip()
temp = soup.select('.tem')[0].text.strip()
return {'weather': weather, 'temp': temp}
print(get_weather('beijing'))
```
这个程序使用requests库发送HTTP请求,然后使用BeautifulSoup库解析HTML文档,获取天气和温度信息。
你可以将这个程序的输出存储到数据库中,以便后续查询。当然,具体实现还需要考虑如何防止被网站封禁、如何处理异常情况等问题。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)