下面是一个示例程序,用于爬取淘宝商品详情数据:
import requests
from bs4 import BeautifulSoup
def get_product_details(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取商品标题
title = soup.select('.tb-main-title')[0].text.strip()
# 获取商品价格
price = soup.select('.tm-price')[0].text.strip()
# 获取商品销量
sales = soup.select('.tm-ind-sellCount .tm-count')[0].text.strip()
# 获取商品评价数
reviews = soup.select('#J_ItemRates .tm-count')[0].text.strip()
# 获取商品详情图片链接
images = [img['src'] for img in soup.select('#J_DivItemDesc img')]
# 输出结果
print('商品标题:', title)
print('商品价格:', price)
print('商品销量:', sales)
print('商品评价数:', reviews)
print('商品详情图片链接:', images)
if __name__ == '__main__':
url = 'https://item.taobao.com/item.htm?spm=a230r.1.14.1.5e112a93k2hQIQ&id=xxxxxx'
get_product_details(url)
在代码中使用了requests
和BeautifulSoup
库,其中requests
用于发送HTTP请求,而BeautifulSoup
用于解析HTML页面。
在运行代码之前,你需要先安装requests
和BeautifulSoup
库,你可以使用以下命令安装:
pip install requests
pip install beautifulsoup4
在代码中,get_product_details
函数接收一个淘宝商品详情页面的URL作为参数,并从该页面中提取商品的标题、价格、销量、评价数和详情图片链接。
你只需要将url
变量替换为你需要爬取的商品详情页面的URL,然后运行代码即可。
请注意,网页的HTML结构可能会随时间而变化,所以你可能需要根据最新的页面结构调整代码。