python爬虫爬取赶集网数据

python爬虫爬取赶集网数据

前期的配置工作在之前的一篇博文中有提到过,现在直接进行爬取

一.创建项目

scrapy startproject putu

二.创建spider文件

1 scrapy genspider  patubole patubole.com

 

三.利用chrome浏览器分析出房价和标题的两个字段的xpath表达式,开始编写patubole.py文件。网络的爬取是通过这个文件进行的

以下代码是最终的代码

所建的patubole.py文件必须实现name,parse函数,start_url这三个属性

 1 # -*- coding: utf-8 -*-
 2 import scrapy
 3 from scrapy.http import Request
 4 from urllib import parse
 5 from patu.items import PatuItem
 6 
 7 
 8 class PatuboleSpider(scrapy.Spider):
 9     name = 'patubole'
10     # allowed_domains = ['python.jobbole.com']
11     start_urls = ['http://xa.ganji.com/fang1/']
12 
13     def parse(self, response):
14         zufang_title=response.xpath('//*[@class="f-list-item ershoufang-list"]/dl/dd[1]/a/text()').extract()
15         zufang_money=response.xpath('//*[@class="f-list-item-wrap f-clear"]/dd[5]/div[1]/span[1]/text()').extract()17         for i,j in zip(zufang_title,zufang_money):
18             print(i,":",j)20 

四.将爬取的数据保存到数据库sufang中。

(1)在pycharm中新建数据库

import sqlite3
zufang=sqlite3.connect('zufang sqlite')
create_table='create table zufang (title varchar(521),money varchar(128))'
zufang.execute(create_table)
exit()

  

完成后会出现

(2)将数据存放在新建的数据库zufang的数据表sufang中

数据的爬取是有patubole.py实现的,数据的存储是由pipelines.py实现的,pipelines.py又是有items.py提供数据的支持

所以编写items.py

 1 # -*- coding: utf-8 -*-
 2 
 3 # Define here the models for your scraped items
 4 #
 5 # See documentation in:
 6 # https://doc.scrapy.org/en/latest/topics/items.html
 7 
 8 import scrapy
 9 
10 
11 class PatuItem(scrapy.Item):
12     # define the fields for your item here like:
13     # name = scrapy.Field()
14     zufang_title=scrapy.Field()
15     zufang_money=scrapy.Field()
16     pass

此时就要回过头来修改刚开是为了测试编写的patubole.py 文件

代码如下

 1 # -*- coding: utf-8 -*-
 2 import scrapy
 3 from scrapy.http import Request
 4 from urllib import parse
 5 from patu.items import PatuItem
 6 
 7 
 8 class PatuboleSpider(scrapy.Spider):
 9     name = 'patubole'
10     # allowed_domains = ['python.jobbole.com']
11     start_urls = ['http://xa.ganji.com/fang1/']
12 
13     def parse(self, response):
14         zufang_title=response.xpath('//*[@class="f-list-item ershoufang-list"]/dl/dd[1]/a/text()').extract()
15         zufang_money=response.xpath('//*[@class="f-list-item-wrap f-clear"]/dd[5]/div[1]/span[1]/text()').extract()
16         pipinstall=PatuItem()   #创建PatuItem实例,实现数据的传递
17         for i,j in zip(zufang_title,zufang_money):
18             pipinstall['zufang_title']=i
19             pipinstall['zufang_money']=j
20 
21             yield pipinstall    #这一步很重要
22 23 24 # pass

 

(3)在settings.py中进行PatuPipeline文件配置

1 ITEM_PIPELINES = {
2    'patu.pipelines.PatuPipeline': 300,
3 }

(5)pipelines.py文件代码,实现存储数据到数据库中

其中包含SQL的相关知识

 1 # Define your item pipelines here
 2 #
 3 # Don't forget to add your pipeline to the ITEM_PIPELINES setting
 4 # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
 5 
 6 import sqlite3
 7 class PatuPipeline(object):
 8     def open_spider(self,spider):
 9         self.con=sqlite3.connect('zufang sqlite')
10         self.cn=self.con.cursor()
11 
12 
13     def process_item(self, item, spider):
14         # print(item.zufang_title,item.zufang_money)
15         insert_sql='insert into sufang (title,money) values("{}","{}")'.format(item['zufang_title'],item['zufang_money'])
16         print(insert_sql)
17         self.cn.execute(insert_sql)
18         self.con.commit()
19         return item
20 
21     def spider_close(self,spider):
22         self.con.close()

最终结果

 

 其中main.py文件是为了调式方便而添加的,可以不用,直接用相关命令启动爬虫

 

posted @ 2018-04-21 17:06 不停地走 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么学爬虫?】        1、爬虫入手容易,但是深入较难,如何写出高效率的爬虫,如何写出灵活性高可扩展的爬虫都是一项技术活。另外在爬虫过程中,经常容易遇到被反爬虫,比如字体反爬、IP识别、验证码等,如何层层攻克难点拿到想要的数据,这门课程,你都能学到!        2、如果是作为一个其他行业的开发者,比如app开发,web开发,学习爬虫能让你加强对技术的认知,能够开发出更加安全的软件和网站 【课程设计】 一个完整的爬虫程序,无论大小,总体来说可以分成三个步骤,分别是:网络请求:模拟浏览器的行为从网上抓取数据数据解析:将请求下来的数据进行过滤,提取我们想要的数据数据存储:将提取到的数据存储到硬盘或者内存中。比如用mysql数据库或者redis等。那么本课程也是按照这几个步骤循序渐进的进行讲解,带领学生完整的掌握每个步骤的技术。另外,因为爬虫的多样性,在爬取的过程中可能会发生被反爬、效率低下等。因此我们又增加了两个章节用来提高爬虫程序的灵活性,分别是:爬虫进阶:包括IP代理,多线程爬虫,图形验证码识别、JS加密解密、动态网页爬虫、字体反爬识别等。Scrapy和分布式爬虫:Scrapy框架、Scrapy-redis组件、分布式爬虫等。通过爬虫进阶的知识点我们能应付大量的反爬网站,而Scrapy框架作为一个专业的爬虫框架,使用他可以快速提高我们编写爬虫程序的效率和速度。另外如果一台机器不能满足你的需求,我们可以用分布式爬虫让多台机器帮助你快速爬取数据。 从基础爬虫到商业化应用爬虫,本套课程满足您的所有需求!【课程服务】 专属付费社群+定期答疑

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值