jtxs0000 发表于 2020-8-31 13:50:31

爬虫求助

爬取汽车之家的,数据也正常拿到手了
但因为有些车子是新车,没有二手价的,导致在保存的时候报错
IndexError: list index out of range
不知道有没有大佬可以帮忙解决下

代码如下:

import parsel
import requests
import openpyxl

def get_url(url):
   
    r = requests.get(url, headers=headers).text
    html = parsel.Selector(r)
   
    # 车辆名称
    name = html.xpath('//div[@class="cards-bottom"]/h4/text()').getall()

    # 里程数
    milage = html.xpath('//div[@class="cards-bottom"]/p/text()').getall()
    print(len(milage))

    # 现价
    price = html.xpath('//div[@class="cards-price-box"]/span/text()').getall()

    print(len(price))

    # 原价
    new_price = html.xpath('//div[@class="cards-price-box"]/s/text()').getall()
    print(len(new_price))
   

    datalist = []
    for i,_ in enumerate(name):
      data = []
      data.append(name)
      data.append(milage.split("/"))
      data.append(milage.split("/"))
      data.append(milage.split("/"))
      data.append(price)
      if price == None:
            continue
      
      data.append(new_price)
      if new_price == None:
            continue

      datalist.append(data)
      
   
    return datalist
         
def toexcel(datalist):
    wb = openpyxl.Workbook()
    wb.guess_type = True
    ws = wb.active

    ws.append(['车辆名称','里程数','上牌时间',"地点","现价","原价"])

   
    for each in datalist:
      
      ws.append(each)

    wb.save("汽车之家.xlsx")


if __name__ == "__main__":
   
    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3760.400 QQBrowser/10.5.4083.400'
}
    page = int(input("请输入你要下载的页数:"))
   
    for i in range(1,page+1):
      url = f"https://www.che168.com/china/a0_0msdgscncgpi1ltocsp{i}exx0/?pvareaid=102179#currengpostion"
      print(url)
      datalist = get_url(url)
      toexcel(datalist)

    print("采集完成^_^")

jtxs0000 发表于 2020-8-31 14:28:07

这贴沉了?{:10_262:}

suchocolate 发表于 2020-8-31 14:29:18

本帖最后由 suchocolate 于 2020-8-31 14:35 编辑

先占个位。

jtxs0000 发表于 2020-8-31 14:32:37

suchocolate 发表于 2020-8-31 14:29
第几页的哪个车没有二手价,我需要看一下页面。

时不时在更新的,有时候有时候会往后跳一点,我主要想解决的是当发现那里没有数据的时候我能不能直接跳过或者随便放点东西在里面填充一下{:10_266:}

suchocolate 发表于 2020-8-31 14:36:55

jtxs0000 发表于 2020-8-31 14:32
时不时在更新的,有时候有时候会往后跳一点,我主要想解决的是当发现那里没有数据的时候我能不能直接跳过 ...

看到了,有的没有原价。这个可能得换一种思路了。
把每个车的info分支提取出来,做一个列表,我帮你写写看。

jtxs0000 发表于 2020-8-31 14:41:57

suchocolate 发表于 2020-8-31 14:36
看到了,有的没有原价。这个可能得换一种思路了。
把每个车的info分支提取出来,做一个列表,我帮你写写 ...

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3760.400 QQBrowser/10.5.4083.400'
}
url = "https://www.che168.com/china/a0_0msdgscncgpi1ltocsp2exx0/?pvareaid=102179#currengpostion"
r = requests.get(url, headers=headers).text
    html = parsel.Selector(r)
   
    # 车辆名称
    name = html.xpath('//div[@class="cards-bottom"]/h4/text()').getall()

    # 里程数
    milage = html.xpath('//div[@class="cards-bottom"]/p/text()').getall()
    print(milage)

    # 现价
    price = html.xpath('//div[@class="cards-price-box"]/span/em/text()').getall()

    print(price)

    # 原价
    new_price = html.xpath('//div[@class="cards-price-box"]/s/text()').getall()
    print(new_price)

jtxs0000 发表于 2020-8-31 14:42:51

suchocolate 发表于 2020-8-31 14:36
看到了,有的没有原价。这个可能得换一种思路了。
把每个车的info分支提取出来,做一个列表,我帮你写写 ...

这里打印的就是列表

疾风怪盗 发表于 2020-8-31 14:50:47

jtxs0000 发表于 2020-8-31 14:42
这里打印的就是列表

你这里一下子就getall获取全部了,一个列表,还不会留空,不好操作的,试试看能不能挨个获取内容,然后再添加进空列表,这样如果获取不到,就用if判断,获取不到的用其他内容替换

suchocolate 发表于 2020-8-31 14:55:28

jtxs0000 发表于 2020-8-31 14:42
这里打印的就是列表

我用的lxml,把car单独取出来,然后逐个遍历,对于ori_price为空的你可以做一个判断。
然后组成data,写到xlsx里。

import requests
from lxml import etree


def main():
    url = 'https://www.che168.com/china/a0_0msdgscncgpi1ltocsp1exx0/?pvareaid=102179#currengpostion'
    headers = {'user-agent': 'firefox'}
    r = requests.get(url, headers=headers)
    html = etree.HTML(r.text)
    result = html.xpath('//a[@class="carinfo"]')
    for item in result:
      name = item.xpath('./div/h4/text()')
      milage = item.xpath('./div/p/text()')
      ori_price = item.xpath('./div/div/s/text()')
      cur_price = item.xpath('./div/div/span/em/text()')
      print(name, milage, ori_price, cur_price)


if __name__ == '__main__':
    main()

jtxs0000 发表于 2020-8-31 15:04:22

suchocolate 发表于 2020-8-31 14:55
我用的lxml,把car单独取出来,然后逐个遍历,对于ori_price为空的你可以做一个判断。
然后组成data,写 ...

谢谢大佬了,就是判断那里卡住了,试了很多方法就是会报错
页: [1]
查看完整版本: 爬虫求助