用python获取1688热销产品并导出excel

您可以使用Python中的pandas库将1688热销产品导出到Excel文件中。以下是一个示例代码:

复制代码

python复制代码

|---|------------------------------------------------------------------|
| | import requests |
| | from bs4 import BeautifulSoup |
| | import pandas as pd |
| | |
| | # 发送HTTP请求 |
| | url = 'https://1688.com/HotProduct/' |
| | response = requests.get(url) |
| | |
| | # 解析HTML页面 |
| | soup = BeautifulSoup(response.text, 'html.parser') |
| | |
| | # 获取热销产品列表 |
| | hot_products = soup.find_all('div', class_='HotProduct-item') |
| | |
| | # 创建DataFrame对象 |
| | data = [] |
| | for product in hot_products: |
| | title = product.find('h1').text.strip() |
| | price = product.find('em').text.strip() |
| | data.append({'Title': title, 'Price': price}) |
| | df = pd.DataFrame(data) |
| | |
| | # 将DataFrame对象导出到Excel文件中 |
| | df.to_excel('hot_products.xlsx', index=False) |

在这个示例代码中,我们首先发送HTTP请求并解析HTML页面,然后使用BeautifulSoup库提取热销产品的标题和价格。接下来,我们创建一个pandas DataFrame对象,将提取到的数据存储在其中。最后,我们使用DataFrame对象的to_excel()方法将数据导出到Excel文件中。

相关推荐
从零开始学习人工智能1 小时前
【踩坑实录】WSL2 解决 onnxruntime\-gpu ImportError: libcudart\.so\.13 无 CUDA13 运行库问题
python
WWJA王文举2 小时前
I²C通信完整流程详解:START、地址、ACK、数据、Repeated START和STOP一次讲透
c语言·开发语言
卷无止境3 小时前
在 awesome-fastapi 里,哪些库值得一看?
后端·python
zhanghaha13143 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
Python私教3 小时前
API 输出模型怎么设计:从 model_dump() 到显式展示层
python·fastapi
Python私教4 小时前
本地 AI 工具服务该绑定 127.0.0.1 还是 0.0.0.0?
python·fastapi
卷无止境4 小时前
FastAPI 的Admin面板生态
后端·python
qq_448011164 小时前
C语言中的动态内存分配
c语言·开发语言·php
ctlover4 小时前
Streamlit 框架
python
ι:5 小时前
MATLAB 与 Python 搭建无人机地面站:优势、劣势与选型逻辑
python·matlab·无人机