个人使用
- User
个人使用
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
个人使用
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
明白了,这是一个简化版的脚本,更适合个人使用,不需要那么多反爬措施:
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
def get_product_info(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
try:
# 获取商品页面
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取商品标题
title = soup.select_one('.sku-name').text.strip()
# 获取商品价格
price = soup.select_one('.price').text.strip()
# 获取SKU信息
sku_list = []
for sku in soup.select('.item'):
sku_name = sku.get('data-value', '')
sku_list.append(sku_name)
return {
'title': title,
'price': price,
'sku_list': sku_list
}
except Exception as e:
print(f"获取商品信息失败: {e}")
return None
def main():
# 输入店铺链接
shop_url = input("请输入京东店铺链接:")
# 存储所有商品数据
all_products = []
try:
# 获取店铺商品列表页
response = requests.get(shop_url)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取所有商品链接
product_links = soup.select('.gl-item .p-img a')
print(f"共发现 {len(product_links)} 个商品")
# 遍历每个商品链接
for link in product_links:
product_url = f"https:{link['href']}"
product_info = get_product_info(product_url)
if product_info:
all_products.append(product_info)
# 简单延迟,避免请求过快
time.sleep(1)
# 保存到Excel
df = pd.DataFrame(all_products)
df.to_excel('京东商品数据.xlsx', index=False)
print("数据已保存到:京东商品数据.xlsx")
except Exception as e:
print(f"程序运行出错: {e}")
if __name__ == "__main__":
main()
这个简化版本的特点:
使用更简单的pandas库保存Excel,不需要openpyxl
移除了复杂的反爬措施,只保留基本的User-Agent和延迟
简化了错误处理
输出更清晰的进度提示
使用方法:
pip install requests beautifulsoup4 pandas
注意事项:
建议不要频繁使用,避免IP被限制
如果遇到获取不到数据,可能需要根据实际页面结构调整选择器
这个脚本主要用于学习参考,实际使用时建议遵守京东的使用条款
如果需要获取大量数据,建议:
需要任何调整或遇到问题都可以继续询问。