爬虫scrapy在爬取数据过程中,由于名字和数据的量不是一致的,怎么将其组合起来?

在爬取https://www.autohome.com.cn/b/%E7%BD%91%E7%AB%99%E7%9A%84%E6%97%B6%E5%80%99
由于汽车名字比指导价多,然后不会一一对应了
(有一些汽车没有指导价格)

img

img

img

怎么将价格跟名字组合起来呢?


```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


```

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:
    1. 首先在scrapy的项目中,找到对应的spider文件,可以尝试使用xpath或者正则表达式来获取汽车名字和指导价,例如:
    def parse(self, response):
        car_names = response.xpath('xpath to get car names').extract()
        guide_prices = response.xpath('xpath to get guide prices').extract()
        # 然后对自动匹配失败的车辆名字进行单独处理
    
    1. 针对那些没有指导价格的车辆名字,可以自行定义一个默认值,例如:
    default_price = '暂无指导价'
    for i in range(len(car_names)):
        if not guide_prices[i]:
            guide_prices[i] = default_price
    
    1. 另外,如果指导价和车名是分别存储在两张表中的,可以考虑通过主键或者外键进行关联操作,例如:
    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来进行数据的匹配和处理
    
    1. 最后对于数据组合的问题,可以采用Python内置的zip函数将两个列表(汽车名字和指导价)进行合并,例如:
    car_data = zip(car_names, guide_prices)
    for item in car_data:
        print('车名:', item[0], '指导价:', item[1])
    

    总之,本题的解决方案需要根据具体的数据结构和业务需求来决定,需要根据实际情况进行调整和优化。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

按块进行爬取,通过xpath定位到对应的块,然后获取名字和价格,如果没有价格自己定义一个默认值或者直接标注“没有值”