爬取三页,对所有信息爬取
时间: 2024-03-06 08:18:17 浏览: 14
好的,我可以使用Python爬虫工具来爬取瓜子二手车网站的商品信息。我会使用requests库获取网页内容,使用BeautifulSoup库解析网页内容,并使用正则表达式和CSS选择器提取所需信息。以下是代码示例:
```python
import requests
from bs4 import BeautifulSoup
import re
url = 'https://www.guazi.com/buy/o{}/#bread'
for i in range(1, 4):
res = requests.get(url.format(i))
soup = BeautifulSoup(res.text, 'html.parser')
items = soup.select('.carlist li a')
for item in items:
href = item['href']
res_item = requests.get(href)
soup_item = BeautifulSoup(res_item.text, 'html.parser')
title = soup_item.select_one('.titlebox h1').text.strip()
price = soup_item.select_one('.pricestype span').text.strip()
mileage = soup_item.select_one('.baseinfo .one').text.strip()
year = soup_item.select_one('.baseinfo .two').text.strip()
location = soup_item.select_one('.baseinfo .three').text.strip()
print(title, price, mileage, year, location)
```
这段代码会爬取瓜子二手车网站前三页的商品信息,并对每个商品提取标题、价格、里程、年份和地点等信息,并打印出来。注意,爬取网站信息需要遵守相关法律法规和网站的爬虫协议,不得进行恶意爬取和商业使用。