爬取淘宝网站的商品数据Python代码
时间: 2023-09-18 11:12:14 浏览: 96
以下是一个简单的 Python 爬取淘宝网站商品数据的示例代码:
import requests
from bs4 import BeautifulSoup
url = 'https://s.taobao.com/search?q=商品名称'
# 设置 User-Agent,避免反爬虫机制
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'}
# 发送 GET 请求,获取 HTML 页面
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析 HTML 页面,提取商品数据
items = soup.find_all('div', class_='item')
for item in items:
title = item.find('a', class_='title').text.strip()
price = item.find('div', class_='price').text.strip()
print(title, price)
其中,需要将 商品名称
替换成您要爬取的商品名称。此外,还需要安装 Requests 和 Beautiful Soup 爬虫库。
相关推荐
















