网络爬虫爬取天气数据
时间: 2024-06-17 09:03:12 浏览: 22
网络爬虫是一种可以自动化地从互联网上获取数据的程序。爬虫可以通过模拟浏览器行为,访问目标网站并抓取所需数据。下面介绍一下如何使用Python编写网络爬虫来爬取天气数据:
1. 首先,需要安装Python的requests和beautifulsoup4模块。在命令行中输入以下命令即可安装:
```
pip install requests
pip install beautifulsoup4
```
2. 打开一个浏览器,访问需要爬取的天气预报网站,例如中国天气网(http://www.weather.com.cn/)。
3. 打开浏览器的开发者工具(一般按F12或者Ctrl+Shift+I快捷键即可),在“Network”标签页中找到请求的URL地址。
4. 利用Python的requests模块发送HTTP请求,获取网页HTML源代码。
```python
import requests
url = 'http://www.weather.com.cn/weather/101010100.shtml'
response = requests.get(url)
html = response.content.decode('utf-8')
```
5. 使用beautifulsoup4模块解析HTML代码,提取所需的天气数据。在这里以获取北京的当天天气预报为例。
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
today = soup.find('div', {'id': 'today'})
weather = today.find('p', {'class': 'wea'}).text
temperature = today.find('p', {'class': 'tem'}).text
```
6. 最后将爬取到的数据输出即可。
```python
print('今日天气:', weather)
print('今日温度:', temperature)
```