Python爬虫采集焦作天气信息并写入excel表
时间: 2024-05-27 07:03:44 浏览: 139
使用python爬虫对天气信息进行爬取
好的,你可以使用Python中的requests库和BeautifulSoup库来实现网页的爬取和数据的解析,再使用openpyxl库将数据写入Excel表中。
首先,你需要找到一个获取焦作天气信息的网站。我这里以“中国天气网”为例,其网址为:http://www.weather.com.cn/weather/101181101.shtml。
接下来,你可以按照以下步骤实现:
1. 导入所需库:
```python
import requests
from bs4 import BeautifulSoup
import openpyxl
```
2. 发送请求获取页面内容:
```python
url = 'http://www.weather.com.cn/weather/101181101.shtml'
response = requests.get(url)
```
3. 解析页面内容:
```python
soup = BeautifulSoup(response.content, 'html.parser')
```
4. 获取所需数据:
```python
city = soup.find('div', class_='crumbs fl').find_all('a')[-1].text
today_weather = soup.find('div', class_='today clearfix').find('p', class_='wea').text
today_temp = soup.find('div', class_='today clearfix').find('p', class_='tem').text.replace('\n', '')
```
5. 创建Excel表格并写入数据:
```python
wb = openpyxl.Workbook()
ws = wb.active
ws['A1'] = '城市'
ws['B1'] = '天气'
ws['C1'] = '温度'
ws.append([city, today_weather, today_temp])
wb.save('weather.xlsx')
```
完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
import openpyxl
url = 'http://www.weather.com.cn/weather/101181101.shtml'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
city = soup.find('div', class_='crumbs fl').find_all('a')[-1].text
today_weather = soup.find('div', class_='today clearfix').find('p', class_='wea').text
today_temp = soup.find('div', class_='today clearfix').find('p', class_='tem').text.replace('\n', '')
wb = openpyxl.Workbook()
ws = wb.active
ws['A1'] = '城市'
ws['B1'] = '天气'
ws['C1'] = '温度'
ws.append([city, today_weather, today_temp])
wb.save('weather.xlsx')
```
执行完毕后,你将在当前目录下生成一个名为“weather.xlsx”的Excel表格。
阅读全文