scrapy框架爬取校花网站的升级版
来源:互联网 发布:wps mac版下载 编辑:程序博客网 时间:2024/05/16 18:48
**spider目录下的文件:定义DemoSpider类**# -*- coding: utf-8 -*-from scrapy.spiders import CrawlSpider,Rulefrom scrapy.linkextractors import LinkExtractorfrom img.items import ImgItem#from bs4 import BeautifulSoup#import urllib#import requestsclass DemoSpider(CrawlSpider): name='demo' start_urls = ['http://www.xiaohuar.com/list-1-2.html'] """第一个Rule是用来筛选所有的网页 第二个是用来搜索当前页面的所有校花的子url allow里面的是正则表达式索引带有这个正则的url restrict_xpaths限定的是搜索的范围 callback回调函数,用来处理页面 process_links用来定义出来url的链接,其中定义的函数要传入参数links follow是用来定义是否跟进 """ rules={Rule(LinkExtractor(allow=('http://www.xiaohuar.com/list'), restrict_xpaths=("//div[@class='page_num']")), #callback="paser_url", follow=True), Rule(LinkExtractor(allow='/p',restrict_xpaths="//div[@class='title']"), callback="paser_item", follow=False ) } def paser_item(self,response): item=ImgItem() url=response.url print "url=%s"%url #检查异常 try: img_url=response.xpath("//div[@class='infoleft_imgdiv']/a/img/@src").extract()[0] name=response.xpath("//div[@class='infodiv']/table/tbody/tr[1]/td[2]/text()").extract() school=response.xpath("//div[@class='infodiv']/table/tbody/tr[5]/td[2]/text()").extract() if 'http://www.xiaohuar.com' not in img_url: item['url'] = 'http://www.xiaohuar.com'+img_url else: item['url']=img_url item['name'] = name item['school'] = school yield item except Exception: print 'error' **定义items文件**import scrapyclass ImgItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() url=scrapy.Field() name=scrapy.Field() school=scrapy.Field() *******定义pipelines文件*********** import codecsimport jsonimport urllibimport osclass ImgPipeline(object): def __init__(self): self.file=codecs.open('items.json','w',encoding='utf-8') #以json文件的方式打开,编码为utf-8,否则会乱码 # self.file_path=os.path.normpath("h:\\scrapy\\img\\img_picture") # self.count=1 def process_item(self, item, spider): #必须实现的函数,用于操作item line=json.dumps(dict(item),ensure_ascii=False)+'\n' #将item中的每一个数据转换成json格式的并且每一个数据都要换行娴熟 # if not os.path.exists(self.file_path): # os.mkdir(self.file_path) # img_name=os.path.normpath("h:\\scrapy\\img\\img_picture\\%s.jpg"%self.count) # urllib.urlretrieve(item['url'],img_name) # self.count+=1 self.file.write(line) return item #最后一般都要返回item,以便后续还要操作item def close_file(self): self.file.close() ***********seetting**************** #在setting文件中加上下面这句话 ITEM_PIPELINES={ "img.pipelines.ImgPipeline":300}
0 0
- scrapy框架爬取校花网站的升级版
- scrapy框架爬取校花网站
- Python爬虫框架Scrapy:爬取校花网
- 用Python 的 Scrapy 爬取 网站
- 基于scrapy框架的关于58同城招聘网站信息的爬取
- scrapy爬取电影网站
- 使用scrapy爬取网站上的所有图片
- 使用Scrapy爬取一个网站的数据
- Scrapy爬取网易云音乐和评论(二、Scrapy框架每个模块的作用)
- scrapy(一)爬取动态网站
- scrapy+xpath爬取不可描述网站
- python+scrapy+mysql爬取故事网站
- python scrapy之爬取 zhengfu网站
- Scrapy 爬虫框架爬取网页数据
- scrapy框架爬虫定时爬取
- scrapy框架爬取51job网
- 【scrapy】爬取框架使用流程(1)
- Scrapy框架爬取详细步骤
- 编程实现禁用HTTP缓存与定时刷新
- IFE糯米学院-checkbox和radio样式的美化实现
- 解决在CentOS6.5下mysql无法远程访问
- mysql 字段类型
- sdutacm-免费馅饼
- scrapy框架爬取校花网站的升级版
- 图解Activity的启动过程
- Web前端-JS效果-导航弹框、背景变色、鼠标移动切换等常见的js效果
- cannot open shared object file: No such file or directory
- web Servlet
- ubuntu深度学习框架Matconvnet与caffe搭建 (Ubuntu14.04+Cuda7.5+Cudnn5+Matlab2015b
- 【BZOJ 2756】[SCOI2012]奇怪的游戏 网络流+二分
- mysql建立索引
- 内存被错误改变,越界写的分析