简单爬虫案例——爬取淘车车信息

网址:aHR0cHM6Ly9jaGFuZ3NoYS55aWNoZS50YW9jaGVjaGUuY29tL2J1eWNhci9wYWdlczlieGNkemFvcXRybm1sLw==

目标:爬取车辆的信息,包括价格,车型,里程,首付等相关信息

按搜索信息找到相应的接口

 

POST请求,需要的载荷为

 

import requests
import csv
f = open('涛车车.csv',mode='w',encoding='utf-8',newline='')
csv_writer = csv.DictWriter(f,fieldnames=[
    "车型",
    '新车售价',
    '首付价格',
    "款式",
    "行驶里程",
    "上架时间",
    "城市",
])
csv_writer.writeheader()
url = 'https://proconsumer.taocheche.com/c-car-consumer/recommend/searchRecommend'
headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
    "Cookie":"auto_id=1af2b3336eb2390682193579b05c8aae; uuid=2e8a0342-2c87-4fb7-989c-a9a8d5808bc0; _utrace=2e8a0342-2c87-4fb7-989c-a9a8d5808bc0; city=%7B%22cityId%22%3A302%2C%22cityName%22%3A%22%E5%8E%A6%E9%97%A8%22%2C%22citySpell%22%3A%22xiamen%22%2C%22cityCode%22%3A%22350200%22%2C%22storeId%22%3A314339%7D; storeId=314339; location_citys=%7B%22cityName%22%3A%22%u957F%u6C99%22%2C%22cityId%22%3A1301%2C%22citySpell%22%3A%22changsha%22%7D; history_citys=%5B%7B%22cityId%22%3A3101%2C%22cityAreaId%22%3A500100%2C%22cityName%22%3A%22%u91CD%u5E86%22%2C%22citySpell%22%3A%22chongqing%22%2C%22uri%22%3A%22/chongqing%22%2C%22hasStore%22%3Anull%2C%22searchText%22%3A%22%u91CD%u5E86chongqing%22%2C%22stores%22%3Anull%2C%22poiCity%22%3Anull%7D%5D; cur_city_new=%7B%22cityId%22%3A3101%2C%22cityAreaId%22%3A500100%2C%22cityName%22%3A%22%u91CD%u5E86%22%2C%22citySpell%22%3A%22chongqing%22%2C%22uri%22%3A%22/chongqing%22%2C%22hasStore%22%3Anull%2C%22searchText%22%3A%22%u91CD%u5E86chongqing%22%2C%22stores%22%3Anull%2C%22poiCity%22%3Anull%7D; t_city=3101; is_choose_city=3101"

}
data = {
    "BusiType": 6,
    "CityId": 3101,
    "ShowCount":-1,
    "platform": "m",
    "userid": "2e8a0342-2c87-4fb7-989c-a9a8d5808bc0"
}
response = requests.post(url=url,json=data,headers=headers)
json_data = response.json()
for index in json_data['data']['carList']:
    dit = {
        "车型": index['brandName'],
        '新车售价':index['NewCarPrice'],
        '首付价格':index['downPayment'],
        "款式":index['carName'],
        "行驶里程":index['drivingMileageText'],
        "上架时间":index['buyCarDateText'],
        "城市":index['cityName'],
    }
    print(dit)
    csv_writer.writerow(dit)

结果展现:

 

 

  • 19
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,您想了解关于Java版Web搜索爬虫系统和Java开源爬虫框架WebCollector的教程,主要是如何爬取搜索引擎的内容,对吗?那我来给您介绍一下。 WebCollector是一款Java开源爬虫框架,它基于Jsoup解析库,提供了一系列简单易用的API,可以帮助用户快速开发爬虫程序。WebCollector可以用于爬取各种类型的网站,包括搜索引擎。 下面是一个简单的WebCollector爬虫程序示例,可以用来爬取百度搜索结果页面的内容: ```java import cn.edu.hfut.dmic.webcollector.model.Page; import cn.edu.hfut.dmic.webcollector.net.HttpRequest; import cn.edu.hfut.dmic.webcollector.net.HttpResponse; import cn.edu.hfut.dmic.webcollector.net.Requester; import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler; public class BaiduSearchCrawler extends BreadthCrawler { public BaiduSearchCrawler(String crawlPath, boolean autoParse) { super(crawlPath, autoParse); this.addSeed("https://www.baidu.com/s?wd=webcollector"); } public void visit(Page page, HttpRequest httpRequest, HttpResponse httpResponse) { System.out.println(page.getHtml()); } public static void main(String[] args) throws Exception { BaiduSearchCrawler crawler = new BaiduSearchCrawler("crawl", true); crawler.setThreads(10); crawler.start(2); } } ``` 在这个示例中,我们定义了一个名为BaiduSearchCrawler的类,它继承自BreadthCrawler类。在BaiduSearchCrawler的构造函数中,我们指定了爬虫程序的爬取路径和是否自动解析网页内容。然后,我们使用addSeed()方法添加了一个种子URL,这个URL是百度搜索webcollector的结果页面。 在visit()方法中,我们定义了爬取页面时的处理逻辑,这里我们只是简单地将页面内容打印出来。在main()方法中,我们创建了一个BaiduSearchCrawler对象,设置了线程数为10,并启动了爬虫程序。 当您运行这个程序时,它将会爬取百度搜索webcollector的结果页面,并将页面内容打印出来。 当然,实际的爬虫程序往往比这个示例要复杂得多,需要考虑到各种情况和异常处理。但是,WebCollector的API非常简单易用,您可以根据自己的需求快速开发出符合要求的爬虫程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力学习各种软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值