7.1.糗事百科

安装

pip install pypiwin32

pip install Twisted-18.7.0-cp36-cp36m-win_amd64.whl

pip install scrapy

创建和运行项目

  1. scrapy startproject qsbk #创建项目
  2.  
  3. scrapy genspider qsbk_spider "qiushibaike.com" #创建爬虫
  4.  
  5. scrapy crawl qsbk_spider #运行爬虫

代码

qsbk_spider.py

  1. # -*- coding: utf-8 -*-
  2. import scrapy
  3. from qsbk.items import QsbkItem
  4.  
  5. class QsbkSpiderSpider(scrapy.Spider):
  6. name = 'qsbk_spider'
  7. allowed_domains = ['qiushibaike.com']
  8. start_urls = ['https://www.qiushibaike.com/8hr/page/1/']
  9. base_domain = "https://www.qiushibaike.com"
  10.  
  11. def parse(self, response):
  12. duanzidivs = response.xpath("//div[@id='content-left']/div")
  13. for duanzidiv in duanzidivs:
  14. author = duanzidiv.xpath(".//h2/text()").get().strip()
  15. content = duanzidiv.xpath(".//div[@class='content']//text()").getall()
  16. content = "".join(content).strip()
  17. item = QsbkItem(author=author,content=content)
  18. yield item
  19. #爬后面页的数据
  20. next_url = response.xpath("//ul[@class='pagination']/li[last()]/a/@href").get()
  21. if not next_url:
  22. return
  23. else:
  24. yield scrapy.Request(self.base_domain+next_url,callback=self.parse)

item.py

  1. import scrapy
  2.  
  3. class QsbkItem(scrapy.Item):
  4. author = scrapy.Field()
  5. content = scrapy.Field()

pipelines.py

  1. # -*- coding: utf-8 -*-
  2.  
  3. import json
  4.  
  5. #1.手动把dick转换成json格式
  6.  
  7. # class QsbkPipeline(object):
  8. # def __init__(self):
  9. # self.fp = open('duanzi.json','w',encoding='utf-8')
  10. #
  11. # def open_spider(self,spider):
  12. # print('开始爬虫')
  13. #
  14. # def process_item(self, item, spider):
  15. # item_json = json.dumps(dict(item),ensure_ascii=False)
  16. # self.fp.write(item_json+'\n')
  17. # return item
  18. #
  19. # def close_spider(self,spider):
  20. # self.fp.close()
  21. # print('爬虫结束了')
  22.  
  23. #2.适用JsonItemExporter,使用与数据量小的情况下
  24. # from scrapy.exporters import JsonItemExporter
  25. # class QsbkPipeline(object):
  26. # def __init__(self):
  27. # self.fp = open('duanzi.json','wb')
  28. # self.exporter = JsonItemExporter(self.fp,ensure_ascii=False,encoding='utf-8')
  29. # self.exporter.start_exporting()
  30. #
  31. # def open_spider(self,spider):
  32. # print('开始爬虫')
  33. #
  34. # def process_item(self, item, spider):
  35. # self.exporter.export_item(item)
  36. # return item
  37. #
  38. # def close_spider(self,spider):
  39. # self.exporter.finish_exporting()
  40. # self.fp.close()
  41. # print('爬虫结束了')
  42.  
  43. #3.JsonLinesItemExporter,适用与数据量大的情况下
  44. from scrapy.exporters import JsonLinesItemExporter
  45. class QsbkPipeline(object):
  46. def __init__(self):
  47. self.fp = open('duanzi.json','wb')
  48. self.exporter = JsonLinesItemExporter(self.fp,ensure_ascii=False,encoding='utf-8')
  49.  
  50. def open_spider(self,spider):
  51. print('开始爬虫')
  52.  
  53. def process_item(self, item, spider):
  54. self.exporter.export_item(item)
  55. return item
  56.  
  57. def close_spider(self,spider):
  58. self.fp.close()
  59. print('爬虫结束了')

settings.py

  1. ROBOTSTXT_OBEY = False
  2.  
  3. DOWNLOAD_DELAY = 1
  4.  
  5. DEFAULT_REQUEST_HEADERS = {
  6. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  7. # 'Accept-Language': 'en',
  8. 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36',
  9. }
  1. ITEM_PIPELINES = {
    'qsbk.pipelines.QsbkPipeline': 300,
    }

start.py

  1. from scrapy import cmdline
  2.  
  3. cmdline.execute("scrapy crawl qsbk_spider".split())

21天打造分布式爬虫-Spider类爬取糗事百科(七)的更多相关文章

  1. 21天打造分布式爬虫-Crawl类爬取小程序社区(八)

    8.1.Crawl的用法实战 新建项目 scrapy startproject wxapp scrapy genspider -t crawl wxapp_spider "wxapp-uni ...

  2. python_爬虫一之爬取糗事百科上的段子

    目标 抓取糗事百科上的段子 实现每按一次回车显示一个段子 输入想要看的页数,按 'Q' 或者 'q' 退出 实现思路 目标网址:糗事百科 使用requests抓取页面  requests官方教程 使用 ...

  3. 芝麻HTTP:Python爬虫实战之爬取糗事百科段子

    首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把,这次我们尝试一下用爬虫把他们抓取下来. 友情提示 糗事百科在前一段时间进行了改版,导致之前的代码没法用了,会导致无法输出和CPU占用过高的 ...

  4. python 爬虫实战1 爬取糗事百科段子

    首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把,这次我们尝试一下用爬虫把他们抓取下来. 本篇目标 抓取糗事百科热门段子 过滤带有图片的段子 实现每按一次回车显示一个段子的发布时间,发布人 ...

  5. Python爬虫实战之爬取糗事百科段子

    首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把,这次我们尝试一下用爬虫把他们抓取下来. 友情提示 糗事百科在前一段时间进行了改版,导致之前的代码没法用了,会导致无法输出和CPU占用过高的 ...

  6. Python爬虫实战之爬取糗事百科段子【华为云技术分享】

    首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把,这次我们尝试一下用爬虫把他们抓取下来. 友情提示 糗事百科在前一段时间进行了改版,导致之前的代码没法用了,会导致无法输出和CPU占用过高的 ...

  7. 爬虫——URL模块爬取糗事百科段子

    最简单的爬取网页找有用信息,难点应该是正则锁定有用信息部分,看了一些其他大神的正则,最后还是决定按照自己理解写一个,果然我头脑相对简单,写出来的粗糙而易理解,也完成了自己想要的需求,就这样了~ # - ...

  8. 爬虫--使用scrapy爬取糗事百科并在txt文件中持久化存储

    工程目录结构 spiders下的first源码 # -*- coding: utf- -*- import scrapy from firstBlood.items import Firstblood ...

  9. python爬虫——利用BeautifulSoup4爬取糗事百科的段子

    import requests from bs4 import BeautifulSoup as bs #获取单个页面的源代码网页 def gethtml(pagenum): url = 'http: ...

随机推荐

  1. 一个域名下多个Vue项目

    公司写的网站要英文和中文的,所以就写了两个项目,都是用vue写的单页面项目,但是域名只有一个,所以就想把两个vue项目合并到一个域名下面.思考:vue的页面都是单页面应用,说白了就是一个index.h ...

  2. 微信小程序之----获取设备信息

    1. 获取系统信息        wx.getSystemInfo(OBJECT)        wx.getSystemInfoSync() 同步获取系统信息                回调常用 ...

  3. CentOS7 安装oracle客户端

    1.本机环境CentOS7 64 [root@localhost etc]# uname -a Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 ...

  4. 写一份简单的webpack2 的配置文件,无比简单

    这是一份自己用到的webpack2的配置写法,从看webpack2开始,发现自己越来越懒了,现在html文件都不想自己写了,直接自己生成... 哈哈,这次是可以无比完美的导入css啦 开发的时候在命令 ...

  5. Log4Net web.config配置

     1 .[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)]  写 ...

  6. spring boot + spring batch 读数据库文件写入文本文件&读文本文件写入数据库

    好久没有写博客,换了一家新公司,原来的公司用的是spring,现在这家公司用的是spring boot.然后,项目组布置了一个任务,关于两个数据库之间的表同步,我首先想到的就是spring batch ...

  7. BZOJ4386[POI2015]Wycieczki / Luogu3597[POI2015]WYC - 矩乘

    Solution 想到边权为$1$的情况直接矩乘就可以得出长度$<=t$ 的路径条数, 然后二分check一下即可 但是拓展到边权为$2$,$3$ 时, 需要新建节点 $i+n$ 和 $i+2n ...

  8. Spring 添加属性集中常见方法

    //创建容器,索要对象, package cn.lijun.Test; import org.junit.Test;import org.springframework.context.Applica ...

  9. tf-slim-mnist

    谷歌开放TF-Slim:在TensorFlow中定义复杂模型的高层库 使用 TF-Slim 的 GitHbu 代码: README:https://github.com/tensorflow/mode ...

  10. MVC 移动识别

    ASP.NET MVC移动端识别 上面我们已经说了 响应式布局,但那是客户端的,针对于同一个视图页面的.不过 同一个视图页面 通过响应式布局 也是有缺点的 会导致页面 样式十分庞大 页面加载效率降低, ...