鸿 鸿 2023-07-10 22:27 采纳率: 92.7%
浏览 17
已结题

某二手车数据获取问题

爬虫爬的结果不一样,重复爬取一页的结果好像,想做到下方教程一样,b站的图灵教程,

img

img

import requests
import time
from lxml import etree
headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,ru;q=0.7',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Origin': 'https://changsha.taoche.com',
    'Pragma': 'no-cache',
    'Referer': 'https://changsha.taoche.com/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',

}


def get_city():

    params = {
    'group': '26',
    }

    response = requests.get(
    'https://proconsumer.taoche.com/c-city-consumer/city/get-filter-city-by-group',
    params=params,
    headers=headers,
    ).json()

    #print(response)
    datas=response
    datas=response['data']
    results=[]
    for data in datas:
        cityList=data['cityList']
        for city in cityList:
            result={
                'cityName':city['cityName'],
                'citySpell':city['citySpell']
                    }
            results.append(result)
    return results

def main():
    cities=get_city()
    print(cities)
    for city in cities:
        results=[]
        for page in range(1,3):
            print(city,page)
            params={
                'page':str(page),
            }
            response=requests.get(f'https://{city["citySpell"]}.taoche.com/all/',params=params,headers=headers,)
            #response = requests.get('https://changsha.taoche.com/all/', params=params, headers=headers)


            content=response.text
            html=etree.HTML(content)
            divs=html.xpath('//*[@id="container_base"]/ul/li/div[@class="gongge_main"]')

            for div in divs:
                title=div.xpath('//*[@id="container_base"]/ul/li/div[@class="gongge_main"]/a/span/text()')
                #titles = ''.join(title)
                #buy_car_time=divs.xpath("/p/i[1]/text()")
                #car_km=divs.xpath("/p/i[2]/text()")
                buy_car_time = div.xpath('//*[@id="container_base"]/ul/li/div[@class="gongge_main"]/p/i[1]/text()')
                car_km = div.xpath('//*[@id="container_base"]/ul/li/div[@class="gongge_main"]/p/i[2]/text()')

                price=div.xpath('./div/i[@class="Total brand_col"]/text()')+div.xpath('./div/i[@class="Total brand_col"]/em/text()')
                prices= ''.join(price)
                result={
                    '名称':title ,

                    '时间':buy_car_time,
                    '公里':car_km,
                    '价格': prices,
                    '城市':city["cityName"]
                }
                print(result)
                results.append(result)
                time.sleep(3)


if __name__ == '__main__':
    main()


  • 写回答

2条回答 默认 最新

  • BsonJ 2023-07-11 01:41
    关注

    被反爬了吧,不然就是参数没设置对。代码跟视频里的一样吗

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月21日
  • 已采纳回答 7月13日
  • 创建了问题 7月10日

悬赏问题

  • ¥15 求帮生成一个lattice diamond的许可证
  • ¥15 大一前端新生求教学解答
  • ¥15 如何制作一个可以查看“网游有序列的装备词条”的软件/插件
  • ¥15 CS2打5E与完美天梯匹配会与服务器断开连接(黑框没标明具体原因)
  • ¥15 利用cst反推材料电磁参数,推出想x,y,z方向的相对介电常数与磁导率
  • ¥15 求帮助!用赛灵思FPGA XC7A35T对一个频率50MHz的数字信号读取高低电平,只用HR bank普通单端io进行采样可以吗
  • ¥15 训练准确率100%,测试准确率只有50%
  • ¥15 grafana创建dashhabord提示no data sources of type Prometheus Alert
  • ¥15 python用arima时间序列法预测不出结果 急
  • ¥15 思科交换机如何恢复配置