Python——爬取瓜子二手车
# coding:utf8
# author:Jery
# datetime:2019/5/1 5:16
# software:PyCharm
# function:爬取瓜子二手车
import requests
from lxml import etree
import re
import csv start_url = 'https://www.guazi.com/www/buy/o1c-1'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
'Cookie': 'uuid=6032a689-d79a-4060-c8b0-f57d8db4e245; antipas=16I7A500578955101K231sG39E; clueSourceCode=10103000312%2300; user_city_id=49; ganji_uuid=3434204287155953305008; sessionid=405f3fb6-fb90-409d-c7ed-32874a157920; lg=1; cainfo=%7B%22ca_s%22%3A%22pz_baidu%22%2C%22ca_n%22%3A%22tbmkbturl%22%2C%22ca_medium%22%3A%22-%22%2C%22ca_term%22%3A%22-%22%2C%22ca_content%22%3A%22%22%2C%22ca_campaign%22%3A%22%22%2C%22ca_kw%22%3A%22-%22%2C%22keyword%22%3A%22-%22%2C%22ca_keywordid%22%3A%22-%22%2C%22scode%22%3A%2210103000312%22%2C%22ca_transid%22%3A%22%22%2C%22platform%22%3A%221%22%2C%22version%22%3A1%2C%22ca_i%22%3A%22-%22%2C%22ca_b%22%3A%22-%22%2C%22ca_a%22%3A%22-%22%2C%22display_finance_flag%22%3A%22-%22%2C%22client_ab%22%3A%22-%22%2C%22guid%22%3A%226032a689-d79a-4060-c8b0-f57d8db4e245%22%2C%22sessionid%22%3A%22405f3fb6-fb90-409d-c7ed-32874a157920%22%7D; cityDomain=mianyang; _gl_tracker=%7B%22ca_source%22%3A%22-%22%2C%22ca_name%22%3A%22-%22%2C%22ca_kw%22%3A%22-%22%2C%22ca_id%22%3A%22-%22%2C%22ca_s%22%3A%22self%22%2C%22ca_n%22%3A%22-%22%2C%22ca_i%22%3A%22-%22%2C%22sid%22%3A20570070983%7D; preTime=%7B%22last%22%3A1556660763%2C%22this%22%3A1556659891%2C%22pre%22%3A1556659891%7D'
} # 获取详情页面url
def get_detail_urls(url):
response = requests.get(url, headers=headers)
text = response.content.decode('utf-8')
html = etree.HTML(text)
index = html.xpath('//ul[@class="pageLink clearfix"]/li[@class="link-on"]/a/span/text()')
next_url = html.xpath('//ul[@class="pageLink clearfix"]/li/a/@href')[-1]
ul = html.xpath('//ul[@class="carlist clearfix js-top"]')[0]
lis = ul.xpath('./li')
urls = []
for li in lis:
detail_url = li.xpath('./a/@href')
detail_url = 'https://www.guazi.com' + detail_url[0]
urls.append(detail_url)
return urls, index, next_url def get_info(url):
response = requests.get(url, headers=headers)
text = response.content.decode('utf-8')
html = etree.HTML(text)
infos_dict = {}
city = html.xpath('//p[@class="city-curr"]/text()')[0]
city = re.search(r'[\u4e00-\u9fa5]+', city).group(0)
infos_dict['city'] = city
title = html.xpath('//div[@class="product-textbox"]/h2/text()')[0]
infos_dict['title'] = title.replace(r'\r\n', '').strip()
infos = html.xpath('//div[@class="product-textbox"]/ul/li/span/text()')
infos_dict['cardtime'] = infos[0]
infos_dict['kms'] = infos[1]
if len(infos) == 4:
infos_dict['cardplace'] = ''
infos_dict['displacement'] = infos[2]
infos_dict['speedbox'] = infos[3]
else:
infos_dict['cardplace'] = infos[2]
infos_dict['displacement'] = infos[3]
infos_dict['speedbox'] = infos[4] price = html.xpath('//div[@class="product-textbox"]/div/span[@class="pricestype"]/text()')[0]
infos_dict['price'] = re.search(r'\d+.?\d+', price).group(0)
return infos_dict def main():
with open(r"C:\Users\Jery\Desktop\guazi.csv", 'w', newline='') as f:
csvwriter_head = csv.writer(f, dialect='excel')
csvwriter_head.writerow(['城市', '车型', '上牌时间', '上牌地', '表显里程', '排量', '变速箱', '价格'])
while True:
global start_url
urls, index, next_url = get_detail_urls(start_url)
print("当前页码:{}*****************".format(index))
# 写表头
with open(r'C:\Users\Jery\Desktop\guazi.csv', 'a') as f:
for url in urls:
print("正在爬取:{}".format(url))
infos = get_info(url)
print(infos)
csvwriter = csv.writer(f, dialect='excel')
csvwriter.writerow(
[infos['city'], infos['title'], infos['cardtime'], infos['cardplace'], infos['kms'],
infos['displacement'],
infos['speedbox'],
infos['price']])
if next_url:
start_url = 'https://www.guazi.com' + next_url if __name__ == '__main__':
main()
后续将进行数据分析
Python——爬取瓜子二手车的更多相关文章
- 使用nodejs的puppeteer库爬取瓜子二手车网站
const puppeteer = require('puppeteer'); (async () => { const fs = require("fs"); const ...
- Python scrapy框架爬取瓜子二手车信息数据
项目实施依赖: python,scrapy ,fiddler scrapy安装依赖的包: 可以到https://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载 pywi ...
- Python 爬取所有51VOA网站的Learn a words文本及mp3音频
Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...
- python爬取网站数据
开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...
- python爬取某个网页的图片-如百度贴吧
python爬取某个网页的图片-如百度贴吧 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用告诉我 #coding:utf-8 import urllib imp ...
- Python:爬取乌云厂商列表,使用BeautifulSoup解析
在SSS论坛看到有人写的Python爬取乌云厂商,想练一下手,就照着重新写了一遍 原帖:http://bbs.sssie.com/thread-965-1-1.html #coding:utf- im ...
- 使用python爬取MedSci上的期刊信息
使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn ...
- python爬取免费优质IP归属地查询接口
python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就o ...
- Python爬取豆瓣指定书籍的短评
Python爬取豆瓣指定书籍的短评 #!/usr/bin/python # coding=utf-8 import re import sys import time import random im ...
随机推荐
- Python3 使用selenium库登陆知乎并保存cookie为本地文件
Python3 使用selenium库登陆知乎并保存cookie为本地文件 学习使用selenium库模拟登陆知乎,并将cookie保存为本地文件,然后供以后(requests模块)使用,用selen ...
- Nhibernate HQL 匿名类(严格说是map的使用以及构造函数的使用
1.map的使用 var hql=string.Format(@"select new map( tc.LimitIndexType as LimitIndexType, tc.LimitS ...
- 一步一步带你构建第一个 Laravel 项目
参考链接:https://laravel-news.com/your-first-laravel-application 简介 按照以下的步骤,你会创建一个简易的链接分享网站. 安装 Laravel ...
- (转)那天有个小孩教我WCF[一][1/3]
原文地址:http://www.cnblogs.com/AaronYang/p/2950931.html 既然是小孩系列,当然要有一点基础才能快速掌握,归纳,总结的一个系列,哈哈 前言: 第一篇嘛,不 ...
- Java Serializable(序列化)的理解和总结
1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存object st ...
- 执行存储过程比即时SQL执行慢的解决方案
发生过这样一件事, 写了一个SQL,查询数据大概5秒,但是放到存储过程里面去了过后,查了5分钟也没给出结果,后来网上找解决方案,终于找到一个解决方案. 在存储过程的参数那里对参数进行一个传递.反正他们 ...
- 搜索引擎Hoot的源码阅读(提供源码)
开门见山,最近阅读了一下一款开源引擎的源码,受益良多(学到了一些套路).外加好久没有写博客了(沉迷吃鸡,沉迷想念姑娘),特别开一篇.Hoot 的源码地址, 原理介绍地址.外加我看过之后的注释版本,当然 ...
- [Essay]看《Re:从零开始的异世界生活》的一些感想
人生不能重来,但动漫可以. -- 故事背景 <介绍背景> 男主486通过不断重来,而改变了剧情的发展.整个动漫就像RPG游戏一般,只看了一遍没有完全理解,但后来再看萌娘百科才把整个剧情里所 ...
- luoguP3690 列队
https://www.luogu.org/problemnew/show/P3960 作为一个初二蒟蒻要考提高组,先做一下17年的题目 我们发现进行一次操作相当于 把第 x 行的第 y 个弹出记为 ...
- opencv学习笔记(四)--图像平滑处理
图像平滑处理的几种常用方法: 均值滤波 归一化滤波 高斯模糊 中值滤波 平滑处理(模糊)的主要目的是去燥声: 不同的处理方式适合不同的噪声图像,其中高斯模糊最常用. 其实最重要的是对图像卷积的核的理解 ...