python爬虫爬取人人车(二手车)、利用padas、matplotlib生成图表,将信息打成csv格式
该程序主要为了抓取人人车卖车信息,包括车系、车型号、购车日期、卖车价格、行驶路程、首付价格等等信息。话不多说直接代码。
入库之后将Mongodb里的信息导出成Excel语句
mongoexport -d myDB -c user -f _id,name,password,adress --csv -o ./user.csv
-d 标示 数据库
-c 标示 数据表
-f 需要提取的field用逗号分隔
-o 输出路径
车系py文件
# -*- coding: utf-8 -*-
import re
from urllib.request import urlopen
from scrapy.http import Request
# from urllib.request import Request
from bs4 import BeautifulSoup
from lxml import etree
import pymongo
import scrapy
from scrapy.selector import HtmlXPathSelector
client = pymongo.MongoClient(host="127.0.0.1")
db = client.renrenche
collection = db.Carclass #表名classification import redis #导入redis数据库
r = redis.Redis(host='127.0.0.1', port=6379, db=0) class renrencheSpider(scrapy.Spider):
name = "Carinfo1"
allowed_domains = ["renrenche.com"] #允许访问的域
start_urls = [
"https://www.renrenche.com/bj/ershouche/"
] #每爬完一个网页会回调parse方法
def parse(self, response):
hxs = HtmlXPathSelector(response)
hx = hxs.select('//div[@class="brand-more-content"]/div[@class="brand-section brand-section-1"]/p[@class="bl"]/span[@class="bn"]/a')
for secItem in hx:
url = secItem.select("@href").extract()
c = "https://www.renrenche.com"+url[0]
name = secItem.select("text()").extract()
classid =self.insertMongo(name,None)
print(c)
print(name)
request = Request(c,callback=lambda response,pid=str(classid):self.parse_subclass(response,pid))
yield request
def parse_subclass(self, response,pid):
# print(response.body.decode('utf-8'))
hxs = HtmlXPathSelector(response)
hx = hxs.select('//ul[@id="filter_series"]/li[@class=""]/a')
for secItem in hx:
urls = secItem.select("@href").extract()
url = "https://www.renrenche.com" + urls[0]
name = secItem.select("text()").extract()
print(url)
print(name)
classid = self.insertMongo(name,pid)
self.pushRedis(classid,url,pid) def insertMongo(self,classname,pid):
classid = collection.insert({'classname':classname,'pid':pid})
return classid
def pushRedis(self,classid,url,pid,):
carurl = '%s,%s,%s' %(classid,url,pid)
r.lpush('carurl',carurl)
卖车各种信息py文件
# -*- coding: utf-8 -*-
import re
from urllib.request import urlopen
from scrapy.http import Request
import pymongo
import scrapy
from time import sleep
from scrapy.selector import HtmlXPathSelector client = pymongo.MongoClient(host="127.0.0.1")
db = client.renrenche
collection = db.Carinfo import redis # 导入redis数据库 r = redis.Redis(host='127.0.0.1', port=6379, db=0) class renrencheSpider(scrapy.Spider):
name = "Carinfo2"
allowed_domains = ["renrenche.com"]
dict = {}
start_urls = [] def __init__(self): # 定义一个方法 a = r.lrange('carurl', 0, -1)
for item in a:
novelurl = bytes.decode(item)
arr = novelurl.split(',') # 分割字符串
renrencheSpider.start_urls.append(arr[1])
pid = arr[0]
url = arr[1]
self.dict[url] = {"pid":pid,"num":0} def parse(self, response): classInfo = self.dict[response.url]
pid = classInfo['pid']
num = classInfo['num']
# print(self.dict)
if num>3:
return None
hxs = HtmlXPathSelector(response)
hx = hxs.select('//ul[@class="row-fluid list-row js-car-list"]')
s=""
for secItem in hx:
hx1 = secItem.select('//li[@class="span6 list-item car-item"]/a[@rrc-event-param="search"]/h3')
name = hx1.select("text()").extract()
a = "型号:"+name[0]
# self.insertMongo(classname=a)
s +=a+"\n"
# classid = collection.insert({'carinfo': a, 'pid': pid})
# print(a)
for secItem in hx:
hx2 = secItem.select('//div[@class="mileage"]/span[@class="basic"]')
name = hx2.select("text()").extract()
b = "购车年份/公里数:"+name[0]+"/"+name[1]
# self.insertMongo(classname1=b)
s +=b+"\n"
# print(b)
for secItem in hx:
hx3 = secItem.select('//div[@class="tags-box"]/div[@class="price"]')
name = hx3.select("text()").extract()
c = str(name[0])
c = c.strip() c = "卖车价格:"+c+"万"
# self.insertMongo(classname2=c)
s +=c+"\n"
# print(c)
for secItem in hx:
hx4 = secItem.select('//div[@class="down-payment"]/div[@class="m-l"]')
name = hx4.select("text()").extract()
d = "首付:"+name[0]+"万"
# self.insertMongo(classname3=d,pid=pid)
s +=d+"\n"
# print(d)
# print(s) arr = s.split('\n')
print(arr[0])
classid = self.insertMongo(arr[0],arr[1],arr[2],arr[3],pid)
# classid = self.insertMongo(s, pid) def insertMongo(self, classname,classname1,classname2,classname3, pid):
classid = collection.insert({'classname': classname,'classname1':classname1,'classname2':classname2,'classname3':classname3, 'pid': pid})
return classid
# r.lpush('novelnameurl', novelnameurl)
python爬虫爬取人人车(二手车)、利用padas、matplotlib生成图表,将信息打成csv格式的更多相关文章
- 用Python爬虫爬取广州大学教务系统的成绩(内网访问)
用Python爬虫爬取广州大学教务系统的成绩(内网访问) 在进行爬取前,首先要了解: 1.什么是CSS选择器? 每一条css样式定义由两部分组成,形式如下: [code] 选择器{样式} [/code ...
- Python爬虫 - 爬取百度html代码前200行
Python爬虫 - 爬取百度html代码前200行 - 改进版, 增加了对字符串的.strip()处理 源代码如下: # 改进版, 增加了 .strip()方法的使用 # coding=utf-8 ...
- 使用Python爬虫爬取网络美女图片
代码地址如下:http://www.demodashi.com/demo/13500.html 准备工作 安装python3.6 略 安装requests库(用于请求静态页面) pip install ...
- Python爬虫|爬取喜马拉雅音频
"GOOD Python爬虫|爬取喜马拉雅音频 喜马拉雅是知名的专业的音频分享平台,用户规模突破4.8亿,汇集了有声小说,有声读物,儿童睡前故事,相声小品等数亿条音频,成为国内发展最快.规模 ...
- python爬虫爬取内容中,-xa0,-u3000的含义
python爬虫爬取内容中,-xa0,-u3000的含义 - CSDN博客 https://blog.csdn.net/aiwuzhi12/article/details/54866310
- Python爬虫爬取全书网小说,程序源码+程序详细分析
Python爬虫爬取全书网小说教程 第一步:打开谷歌浏览器,搜索全书网,然后再点击你想下载的小说,进入图一页面后点击F12选择Network,如果没有内容按F5刷新一下 点击Network之后出现如下 ...
- python爬虫—爬取英文名以及正则表达式的介绍
python爬虫—爬取英文名以及正则表达式的介绍 爬取英文名: 一. 爬虫模块详细设计 (1)整体思路 对于本次爬取英文名数据的爬虫实现,我的思路是先将A-Z所有英文名的连接爬取出来,保存在一个cs ...
- 一个简单的python爬虫,爬取知乎
一个简单的python爬虫,爬取知乎 主要实现 爬取一个收藏夹 里 所有问题答案下的 图片 文字信息暂未收录,可自行实现,比图片更简单 具体代码里有详细注释,请自行阅读 项目源码: # -*- cod ...
- python爬虫-爬取百度图片
python爬虫-爬取百度图片(转) #!/usr/bin/python# coding=utf-8# 作者 :Y0010026# 创建时间 :2018/12/16 16:16# 文件 :spider ...
随机推荐
- php获取当前月与上个月月初及月末时间戳的方法
php 获取今日.昨日.上周.本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime.下面首先还是直奔主题以示例说明如何使用 mktime 获取今日.昨日.上周.本月的起始 ...
- 读懂源码:一步一步实现一个 Vue
源码阅读:究竟怎样才算是读懂了? 市面上有很多源码分析的文章,就我看到的而言,基本的套路就是梳理流程,讲一讲每个模块的功能,整篇文章有一大半都是直接挂源码.我不禁怀疑,作者真的看懂了吗?为什么我看完后 ...
- 深入理解php内核 编写扩展 I:介绍PHP和Zend
内容: 编写扩展I - PHP和Zend起步 原文:http://devzone.zend.com/public/view/tag/Extension Part I: Introduction to ...
- 深入理解javascript函数进阶系列第一篇——高阶函数
前面的话 前面的函数系列中介绍了函数的基础用法.从本文开始,将介绍javascript函数进阶系列,本文将详细介绍高阶函数 定义 高阶函数(higher-order function)指操作函数的函数 ...
- sublime text 3.0新版本注册码
-– BEGIN LICENSE -– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...
- Java设计模式之单例模式详解
在Java开发过程中,很多场景下都会碰到或要用到单例模式,在设计模式里也是经常作为指导学习的热门模式之一,相信每位开发同事都用到过.我们总是沿着前辈的足迹去做设定好的思路,往往没去探究为何这么做,所以 ...
- PHPstorm 如何新增项目
如何在PHPstorm新增项目 1.打开设置 2.找到Directories ,点击增加路径
- 网页设计——2. html入门
开始正式的课程讲解了,首先来看看课程体系: Java EE(java 企业应用程序版本) java2 有三个版本:J2 SE(标准版),J2 EE(企业版).J2 ME(微缩版). 我们要掌握J2EE ...
- setTimeout和setInterval和单线程
我们知道,js是单线程执行的(单线程j就是说在程序执行时,所走的程序路径按照连续顺序排下来,前面的必须处理好,后面的才会执行).所以其实setTimeout和setInterval所谓的"异 ...
- 【004】【JVM——垃圾收集算法】
Java虚拟机学习总结文件夹 垃圾收集算法 垃圾收集算法的实现涉及大量的程序细节,并且各个平台的虚拟机操作内存的方法又各不同样,介绍几种垃圾收集算法的思想及其发展过程. 标记-清除算法 垃圾收集 ...