爬取过程中 遇见 百度蜘蛛反爬 robot.txt,我们可以在scrapy 的setting.py 配置文件下配置

ROBOTSTXT_OBEY = False

最终代码

# -*- coding: utf-8 -*-
from scrapy.spider import Spider
from scrapy.contrib.spiders import CrawlSpider, Rule
#from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.linkextractors import LinkExtractor
from scrapy.selector import Selector
from scrapy.http import Request, HtmlResponse
from 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):returnseen = 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 = 1allowed_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_numberqa_number=qa_number+1;item["number"]=qa_numberprint "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 第" + 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 json
import 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

scrapy 爬取百度知道,多spider子一个项目中,使用一个pielines相关推荐

  1. 经典爬虫:用Scrapy爬取百度股票

    前言 今天我们编写一个用 Scrapy 框架来爬取百度股票的代码,之前写过一篇爬取百度股票的文章(点我),代码的逻辑和这篇文章的逻辑是一样的,用到的解析器不同罢了. Scrapy 爬虫框架 Scrap ...

  2. python爬百度新闻_13、web爬虫讲解2—Scrapy框架爬虫—Scrapy爬取百度新闻,爬取Ajax动态生成的信息...

    crapy爬取百度新闻,爬取Ajax动态生成的信息,抓取百度新闻首页的新闻rul地址 有多网站,当你浏览器访问时看到的信息,在html源文件里却找不到,由得信息还是滚动条滚动到对应的位置后才显示信息, ...

  3. 三十一、Scrapy爬取百度图片

    @Author:Runsen Runsen近段时间进入Scrapy,写了几个爬虫练练手,就找百度图片入手了.本文就是Scrapy的入门文章. 文章目录 目标 创建项目 分析逻辑 代码 目标 爬取 百度 ...

  4. python tkinter界面 多进程启动scrapy爬取百度贴吧的回复,显示爬取进度,并可以搜索回帖人,指定时间生成词云图,用pyinstaller打包成exe(七)

    爬取单个帖子one_tiezi_spider.py ''' 这个是爬取 单个帖子的爬虫 大概思路:          1.进入该帖子的第1页,获取帖子的初始信息(标题.发帖人.tid.总页数)等    ...

  5. Scrapy 爬取百度贴吧指定帖子的发帖人和回帖人

    转载请注明出处:http://blog.csdn.net/gamer_gyt 博主微博:http://weibo.com/234654758 Github:https://github.com/thi ...

  6. Scrapy爬取豆瓣图书Top250数据,在PowerBI中可视化分析

    文章目录 项目说明 Scrapy框架 网页分析 爬虫代码 items spiders pipelines main 爬取结果 PowerBI分析 分析结果 项目说明 近期在学习Python爬虫,看了很 ...

  7. Scrapy爬取斗破苍穹漫画

    Scrapy爬取斗破苍穹漫画 文章目录 Scrapy爬取斗破苍穹漫画 前言 一.创建项目.创建爬虫 二.实战 1.items.py如下: 2.settings.py如下: 3.pipelines.py ...

  8. scrapy爬取站长素材

    scrapy爬取站长素材: 1.创建项目scrapy startproject 爬虫项目名字2.创建虫子scrapy genspider 虫名字3.setting里面加UA伪装4.加LOG_LEVEL ...

  9. 二、入门爬虫,爬取百度图片

    什么是爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模 ...

  10. python爬虫爬取百度图片总结_爬虫篇| 爬取百度图片(一)

    什么是爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模 ...

最新文章

  1. WebServiceHost 在ConsoleApplication中使用时添加命名空间添加不上,报错
  2. 畅通工程再续_MST(hdu 1875)
  3. 75-商品服务-品牌分类关联与级联更新
  4. appium+python自动化项目实战(二):项目工程结构
  5. 巧用 Trie 树,实现搜索引擎关键词提示功能
  6. LeetCode633 | Sum of Square Numbers (Easy)
  7. c语言 字符串 正序再倒序_python字符串
  8. python变量类型字符串的内建函数使用
  9. Java中Date和Calender类的使用方法
  10. 资源重复 uac.res resource kept(转)
  11. 【理财】【学校财务信息管理系统】一卡通网络金融化
  12. pytorch ocr_使用PyTorch解决CAPTCHA(不使用OCR)
  13. Properties与ResourceBundle的基本使用以及区别
  14. win10电脑风扇一直转解决方法
  15. VIOS挂载ISO文件
  16. MATLAB之绘图基础
  17. 农历虎年快到了,我用 Python 写副春联恭祝大家幸福平安
  18. MATLAB——根轨迹原理及其Matlab绘制
  19. 游戏3D美术设计师前景怎么样?
  20. 为什么rand()每次产生的随机数都一样

热门文章

  1. pytorch 训练过程acc_pytorch应用(四)训练过程可视化visdom
  2. winform获取appconfig配置文件得配置
  3. android 停止服务执行,android - 为什么在停止服务(执行onDestroy已执行)后,服务中的变量没有“重置”?...
  4. CSDN自定义模块内容编写
  5. 帧率常量15可以随便改,问为什么是15,是想暴露自己不懂编程?
  6. 博客积分为0,这是又出错了?
  7. 液晶显示器模糊的照片
  8. OPPO推送:推送消息的字串,用于参数
  9. 爬虫获取::after_这种反爬虫手段有点意思,看我破了它!
  10. 开关电源适配器原理_迅为i.MX6ULL开发板原理图分析介绍