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 ...
随机推荐
- thinkphp框架知识点
基本配置 define('APP_DEBUG',true);//开启debug模式 //记录日志 'LOG_RECORD' => true, //系统日志在记录的时候需要开启debug调试模式, ...
- linux grep 从入门到精通
linux grep 从入门到精通 一.初级 搜索日志 grep "186" catalina.out 在新输出日志中监听固定字符串 tail -f catalina.out | ...
- 51Nod 1267 4个数和为0 二分
给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出"Yes",否则输出"No".Input第1行,1个数N,N为数组的长度(4 <= ...
- 可以在手机上看电脑本地html步骤,我自己总结的哦!
1.打开控制面板 2.打开程序和功能 3.打开或关闭功能 4.internet信息服务展开后里面所有的都要选中 5.回到桌面,然后右键计算机,选择'管理' 6.先在E盘或者D盘创建一个文件夹,自己随意 ...
- SpringMvc+JavaConfig+Idea 搭建项目
1.介绍 之前搭建SpringMvc项目要配置一系列的配置文件,比如web.xml,applicationContext.xml,dispatcher.xml.Spring 3.X之后推出了基于Jav ...
- macOS安装beego的使用bee命令出现killed:9 解决办法
最近想搞一个restful api,发现go还是不错的,拿来研究下 beego文档:https://beego.me/quickstart 安装之后,执行bee命令,报错如下: 应该是最新mac OS ...
- MyEclipse 使用图文详解
引言 某天在群里看到有小伙伴问MyEclipse/Eclipse的一些使用问题,虽然在我看来,问的问题很简单,但是如果对于刚刚学习的人来说,可能使用就不那么友好了.毕竟我在开始使用MyEclipse/ ...
- QWT与QT Designer
QWT是一套非常不错的开发库,它能结合QT开发,做出非常好的曲线,刻度,表盘等效果来. qwt的下载以及动态链接库的编译等这里就不做介绍了.在源码目录下可以找到designer目录,其中有插件的源码 ...
- 八皇后問題 (C語言递归實現 回溯法)
八皇后问题是一个以国际象棋为背景的问题:怎样可以在 8×8 的国际象棋棋盘上放置八个皇后,使得不论什么一个皇后都无法直接吃掉其它的皇后?为了达到此目的.任两个皇后都不能处于同一条横行.纵行或斜线上.現 ...
- Asp.net mvc 知多少(四)
本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...