Python打开网页操作后查询,然后把查询结果抓到excel中的代码
时间: 2024-06-10 22:09:16 浏览: 17
下面是Python的代码:
```python
import requests
from bs4 import BeautifulSoup
import openpyxl
# 打开网页并查询结果
url = 'https://www.example.com'
params = {'query': 'search key'}
response = requests.get(url, params=params)
soup = BeautifulSoup(response.text, 'html.parser')
results = soup.find_all('div', class_='result')
# 将查询结果写入Excel文件
workbook = openpyxl.Workbook()
worksheet = workbook.active
for index, result in enumerate(results):
title = result.find('h3').text
link = result.find('a')['href']
description = result.find('p').text
worksheet.cell(row=index+1, column=1, value=title)
worksheet.cell(row=index+1, column=2, value=link)
worksheet.cell(row=index+1, column=3, value=description)
workbook.save('results.xlsx')
```
希望对您有帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)