前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python分布式爬虫打造搜索引擎Scrapy精讲

Python分布式爬虫打造搜索引擎Scrapy精讲

作者头像
拓荒者
发布2019-03-11 11:03:40
8290
发布2019-03-11 11:03:40
举报
文章被收录于专栏:运维经验分享运维经验分享

1、chrome谷歌浏览器无界面运行

chrome谷歌浏览器无界面运行,主要运行在Linux系统,windows系统下不支持

chrome谷歌浏览器无界面运行需要一个模块,pyvirtualdisplay模块

需要先安装pyvirtualdisplay模块

Display(visible=0, size=(800, 600))设置浏览器,visible=0表示不显示界面,size=(800, 600)表示浏览器尺寸

代码语言:javascript
复制
# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request,FormRequest
from selenium import webdriver                  # 导入selenium模块来操作浏览器软件
from scrapy.xlib.pydispatch import dispatcher   # 信号分发器
from scrapy import signals                      # 信号

class PachSpider(scrapy.Spider):                            #定义爬虫类,必须继承scrapy.Spider
    name = 'pach'                                           #设置爬虫名称
    allowed_domains = ['www.taobao.com']                    #爬取域名

    def __init__(self):                                                                                 #初始化

        from pyvirtualdisplay import Display
        display = Display(visible=0, size=(800, 600))
        display.start()

        self.browser = webdriver.Chrome(executable_path='H:/py/16/adc/adc/Firefox/chromedriver.exe')    #创建谷歌浏览器对象
        super(PachSpider, self).__init__()                                                              #设置可以获取上一级父类基类的,__init__方法里的对象封装值
        dispatcher.connect(self.spider_closed, signals.spider_closed)       #dispatcher.connect()信号分发器,第一个参数信号触发函数,第二个参数是触发信号,signals.spider_closed是爬虫结束信号

        #运行到此处时,就会去中间件执行,RequestsChrometmiddware中间件了

    def spider_closed(self, spider):                                        #信号触发函数
        print('爬虫结束 停止爬虫')
        self.browser.quit()                                                 #关闭浏览器

    def start_requests(self):    #起始url函数,会替换start_urls
        return [Request(
            url='https://www.taobao.com/',
            callback=self.parse
        )]


    def parse(self, response):
        title = response.css('title::text').extract()
        print(title)
复制代码
复制代码
复制代码
复制代码

 注意:Linux系统下会出现错误

报错:easyprocess.EasyProcessCheckInstalledError: cmd=['Xvfb', '-help'] OSError=[Errno 2] No such file or directory

需要两个步骤解决

  1.执行命令:sudo apt-get install xvfb    安装xvfb软件

  2.执行命令:pip install xvfbwrapper   安装xvfbwrapper模块

以下只是提到一下,前面讲的selenium模块操作浏览器已经够用了

2、scrapy-splash,也是scrapy获取动态网页的方案,这里就不介绍了,详情:https://github.com/scrapy-plugins/scrapy-splash

3、splinter,是一个操作浏览器的模块 详情:https://github.com/cobrateam/splinter

标签: python 爬虫

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/09/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
消息队列 TDMQ
消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档