Scrapy爬虫(二)——自定义Item和代理访问的爬虫
来源:互联网 发布:sqlserver存储过程if 编辑:程序博客网 时间:2024/05/07 21:32
前言
在Scrapy爬虫(一)——你的第一个Scrapy爬虫中我们写了一个最简易的爬虫,可是这个爬虫并没有实现保存页面内容的功能。
本篇主要会通过pipLine实现页面内容保存以及代理访问的功能。
Item
在每次调用parse()函数都通过yield语句返回一个list,而实际上我们可以自定义一个Item类通过这个函数返回一个Item List。
首先,我们要在新建一个item.py并且自定义一个Item:
class CollectItem(scrapy.Item): news_id=scrapy.Field() language=scrapy.Field() request_url=scrapy.Field() title=scrapy.Field() classification=scrapy.Field() body=scrapy.Field()
这个类要继承于scrapy.Item,里面每个自定义的变量都要等于scrapy.Field()才能发挥作用。而后在parse()中方法中要这样定义:
item['news_id'] = self.cotitem['language'] = 'ind'item['classification'] = response.xpath("//a[@class='label label-primary']/text()").extract()
而后我们自定义的item就会传到pipline.py中的process_item这个方法里面
PipLine
项目管道(Item Pipeline),负责处理有蜘蛛从网页中抽取的项目,他的主要任务是清晰、验证和存储数据。当页面被蜘蛛解析后,将被发送到项目管道,并经过几个特定的次序处理数据。
具体定义如下:
class LearnscrapyPipeline(object): def process_item(self, item, spider): filename="/home/lhn/Desktop/json/"+str(item['news_id'])+".json" with open(filename,"wb")as f: f.write('\"news_id\":'+str(item['news_id'])+'\n') f.write('\"pub_time\":'+''.join(item['pub_time'])+'\n') return item
最后要在setting.py中添加
# Configure item pipelines# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.htmlITEM_PIPELINES = { 'learnScrapy.pipelines.LearnscrapyPipeline': 300,}
下载保存页面内容如此便实现了。
代理访问
当我们爬取一些网站的时候可能需要用到代理服务器,而代理的功能主要是用自定义的DownloadMiddware来实现的。
我们新建一个proxymiddware.py文件
class ProxyMiddleware(object): proxyList = [ \ '127.0.0.1:393' ] def process_request(self, request, spider): # Set the location of the proxy pro_adr = random.choice(self.proxyList) print("USE PROXY -> " + pro_adr) request.meta['proxy'] = "http://" + pro_adr url = request.url; user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; windows NT)' headers = {'User-Agent': user_agent} r = requests.post(url, headers=headers) body = r.content return HtmlResponse(url, encoding='utf-8', status=200, body=body)
我们可以在proxyList中添加任意多的地址,该方法可以随机选择服务器地址来访问。
最后需要再一次在settingups.py里面再次添加配置
# Enable or disable downloader middlewares# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.htmlDOWNLOADER_MIDDLEWARES = { 'learnScrapy.proxymiddleware.ProxyMiddleware': 100}
便可以实现代理访问了。
阅读全文
0 0
- Scrapy爬虫(二)——自定义Item和代理访问的爬虫
- Scrapy爬虫(一)——你的第一个Scrapy爬虫
- scrapy专利爬虫(二)——请求相关
- 浅谈 Scrapy 爬虫(二)
- Scrapy爬虫:模拟浏览器和使用代理
- Scrapy爬虫(二):爬虫简介
- Python 爬虫6——Scrapy的安装和使用
- scrapy爬虫之Item Pipeline
- 爬虫Scrapy-04Item Pipeline
- Scrapy爬虫(三)——简易动态网页爬虫
- 网络爬虫系列笔记(4)——Scrapy爬虫
- Scrapy——可配置的爬虫
- Scrapy——可配置的爬虫
- 用 Scrapy+Mariadb 实现汉典数据爬虫(二)——Scrapy简单介绍
- 爬虫总结(二)-- scrapy
- scrapy专利爬虫(一)——scrapy简单介绍
- Scrapy爬虫:代理IP配置
- Python 爬虫 —— scrapy
- python 在linux中把doc转换为docx格式文件(支持word97和word2003)
- Mac 下安装python PIL
- ssh中分页
- AndroidStudio 通过svn 主干代码合并到分支代码,分支代码合并到主干代码
- Qt动态加载动态库
- Scrapy爬虫(二)——自定义Item和代理访问的爬虫
- html.replace is not a function
- 最长上升子序列 HDU 1025 Constructing Roads In JGShining's Kingdom
- 数据结构基础 排序
- 子网掩码
- Makefile简易教程
- linux目录相关
- js提交后台请求的方式
- c++ 11 特性