利用python来写一个爬取天猫数据的程序
时间: 2024-04-23 21:22:40 浏览: 26
好的,以下是一个简单的爬取天猫数据的 Python 程序:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://list.tmall.com/search_product.htm?q=iphone&type=p&vmarket=&spm=a220m.1000858.a2227oh.d100&from=mallfp..pc_1_searchbutton'
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.36'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
items = soup.find_all('div', {'class': 'product-item'})
for item in items:
title = item.find('p', {'class': 'productTitle'}).text.strip()
price = item.find('p', {'class': 'productPrice'}).text.strip()
print(title + ' - ' + price)
```
这个程序可以爬取天猫上所有 iPhone 的商品信息,并打印出商品的标题和价格。当然,你可以根据自己的需求修改程序中的 URL 和爬取内容。