scrapy 爬取百度知道,多spider子一个项目中,使用一个pielines
来源:互联网 发布:怎么修改淘宝会员名字 编辑:程序博客网 时间:2024/06/06 22:01
爬取过程中 遇见 百度蜘蛛反爬 robot.txt,我们可以在scrapy 的setting.py 配置文件下配置
ROBOTSTXT_OBEY = False
最终代码
# -*- coding: utf-8 -*-from scrapy.spider import Spiderfrom scrapy.contrib.spiders import CrawlSpider, Rule#from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractorfrom scrapy.linkextractors import LinkExtractorfrom scrapy.selector import Selectorfrom scrapy.http import Request, HtmlResponsefrom scrapy import logfrom items import BDzdItemclass BDzdSpider(CrawlSpider): global qa_number; qa_number=0; """爬取百度知道 银行""" log.msg("log",level=log.DEBUG) def _requests_to_follow(self, response): if not isinstance(response, HtmlResponse): return seen = set() for n, rule in enumerate(self._rules): links = [lnk for lnk in rule.link_extractor.extract_links(response) if lnk not in seen] if links and rule.process_links: links = rule.process_links(links) for link in links: if link.text.find("银行") == -1: continue; seen.add(link) r = Request(url=link.url, callback=self._response_downloaded) r.meta.update(rule=n, link_text=link.text) yield rule.process_request(r) name = "bankSpider" download_delay = 1 allowed_domains = ["zhidao.baidu.com"] start_urls = [ "https://zhidao.baidu.com/question/1796062605517856547.html?fr=iks&word=%D2%F8%D0%D0&ie=gbk" ] rules = [ Rule(LinkExtractor(allow=('/question/.*'), restrict_xpaths=('//a[@class="related-link"]')), callback='parse_item', follow=True) ] def parse_item(self, response): #return; # open("aa.txt", 'wb').write(response.body) sel = Selector(response) url=response._url; question=sel.xpath('//span[@class="ask-title "]/text()').extract() answer = sel.xpath('//pre[@class="best-text mb-10"]/text()').extract() otherAnswer=sel.xpath('//div[@class="answer-text line"]/span/text()').extract() #sites=sel.xpath('//a[@class="related-link"]') item = BDzdItem() item["question"] = ''.join(question); if len(answer) > 0: item["answer"] = ''.join(answer);#因为xpath text()截出来可能是字符数组,要转成字符 elif len(otherAnswer) > 0: item["answer"] = ''.join(otherAnswer[0]); else: return; global qa_number qa_number=qa_number+1; item["number"]=qa_number print "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 第" + str(qa_number)+" 条"; print "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" + url; print "##########################################" + item["question"]; print "*******************************************" + item["answer"]; yield item
如果有多个spider在一个项目中,可以在pipelines.py中这样写
# -*- coding: utf-8 -*-# Define your item pipelines here## Don't forget to add your pipeline to the ITEM_PIPELINES setting# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.htmlimport jsonimport codecsclass TutorialPipeline(object): def process_item(self, item, spider): print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%0" return itemclass BDzdPipeline(object): def __init__(self): self.bankFile = codecs.open('data_bank.json', 'wb', encoding='utf-8')#银行 self.mobileFile = codecs.open('data_mobile.json', 'wb', encoding='utf-8')#移动 self.baoxianFile = codecs.open('data_baoxian.json', 'wb', encoding='utf-8')#保险 self.jinrongFile = codecs.open('data_jinrong.json', 'wb', encoding='utf-8')#金融 def process_item(self, item, spider): line = json.dumps(dict(item)) + '\n' if spider.name=='bankSpider': self.bankFile.write(line.decode("unicode_escape")) elif spider.name == 'mobileSpider': self.mobileFile.write(line.decode("unicode_escape")) elif spider.name == 'baoxianSpider': self.baoxianFile.write(line.decode("unicode_escape")) elif spider.name == 'jinrongSpider': self.jinrongFile.write(line.decode("unicode_escape")) return item
0 0
- scrapy 爬取百度知道,多spider子一个项目中,使用一个pielines
- 使用Scrapy爬取一个网站的数据
- scrapy中spider
- 一个百度知道
- 使用python多线程实现一个简单spider
- 项目中引入webMagic爬取一个网页
- Scrapy爬取百度图片(一)
- Scrapy爬取百度图片(二)
- 爬取百度百科[scrapy启发]
- 一个简单的spider
- 使用scrapy中crawlspider爬取csdn文章
- scrapy源码分析(四)-------spider篇------网页爬取流程分析(一)
- 一个项目中使用多个StoryBoard
- Scrapy项目一:爬取电影资讯
- 【Scrapy】学习记录1_一个基本的Scrapy项目
- Scrapy框架学习(一)----Scrapy介绍及第一个项目
- 新建一个scrapy项目的步骤
- 关于一个项目中引入子项目的问题
- 单例模式
- Android Studio 发布APK时报错:input jar file is specified twice
- Java中获取当前系统年份
- C#如何统计代码行数
- MVC封装(六)视图层
- scrapy 爬取百度知道,多spider子一个项目中,使用一个pielines
- FreeCodeCamp中文网练习代码7
- 控制台查看Sqlite的数据
- 748CSanta Claus and Robot
- PackageManager详解
- alloca、malloc、calloc、realloc的详解
- SpringBoot整合Mybatis
- 在VMware10下安装VxWorks6.6虚拟机教程(上篇——准备环节)
- Windows环境下搭建机器学习玩flappy bird(2)