在爬取https://www.autohome.com.cn/b/%E7%BD%91%E7%AB%99%E7%9A%84%E6%97%B6%E5%80%99
由于汽车名字比指导价多,然后不会一一对应了
(有一些汽车没有指导价格)
怎么将价格跟名字组合起来呢?
```python
import scrapy
class QichezhijiaSpider(scrapy.Spider):
name = "qichezhijia"
allowed_domains = ["www.autohome.com.cn"]
start_urls = ["http://www.autohome.com.cn/b/"]
def parse(self, response):
print('墨迹')
content = response.text
print(content)
name_list = response.xpath('//ul[@class="rank-list-ul"]/li/h4/a/text()')
name_list= name_list.extract()
price_list = response.xpath('//ul[@class="rank-list-ul"]/li/div/a[@class="red"]/text()')
price_list = price_list.extract()
print(name_list)
print(price_list)
pass
```
不知道你这个问题是否已经解决, 如果还没有解决的话:def parse(self, response):
car_names = response.xpath('xpath to get car names').extract()
guide_prices = response.xpath('xpath to get guide prices').extract()
# 然后对自动匹配失败的车辆名字进行单独处理
default_price = '暂无指导价'
for i in range(len(car_names)):
if not guide_prices[i]:
guide_prices[i] = default_price
def parse(self, response):
cars = []
for sel in response.xpath('xpath to get car info'):
car = CarItem()
car['name'] = sel.xpath('xpath to get car name').extract_first().strip()
car['guide_price'] = sel.xpath('xpath to get guide price').extract_first().strip()
cars.append(car)
# 在后续处理中,可以基于car.name来进行数据的匹配和处理
car_data = zip(car_names, guide_prices)
for item in car_data:
print('车名:', item[0], '指导价:', item[1])
总之,本题的解决方案需要根据具体的数据结构和业务需求来决定,需要根据实际情况进行调整和优化。
按块进行爬取,通过xpath定位到对应的块,然后获取名字和价格,如果没有价格自己定义一个默认值或者直接标注“没有值”