资讯专栏INFORMATION COLUMN

Scrapy+Chromium+代理+selenium

Pocher / 2166人阅读

摘要:通常的解决办法是通过抓包,然后查看信息,接着捕获返回的消息。为了减少因为安装环境所带来的烦恼。代理因为我们已经用替换了。我们需要直接用来处理代理问题。根据上面这段代码,我们也不难猜出解决代理的方法了。

上周说到scrapy的基本入门。这周来写写其中遇到的代理和js渲染的坑。

js渲染

js是爬虫中毕竟麻烦处理的一块。通常的解决办法是通过抓包,然后查看request信息,接着捕获ajax返回的消息。
但是,如果遇到一些js渲染特别复杂的情况,这种办法就非常非常的麻烦。所以我们采用了selenium这个包,用它来调用chromium完成js渲染的问题。

安装

安装selenium

安装chromium

安装chromium-drive

tip:为什么选择chromium而不是chrome。我之前装的就是chrome。但是安装chrome之后还需要安装chrome-drive,而很多linux发行版的包管理没有现成的chrome包和chrome-drive包,自己去找的话很容易出现chrome-drivechrome版本不一致而导致不能使用。

为了减少因为安装环境所带来的烦恼。我们这边用docker来解决。
Dockerfile

FROM alpine:3.8
COPY requirements.txt /tmp
RUN apk update 
    && apk add --no-cache xvfb python3 python3-dev curl libxml2-dev libxslt-dev libffi-dev gcc musl-dev 
    && apk add --no-cache libgcc openssl-dev chromium=68.0.3440.75-r0 libexif udev chromium-chromedriver=68.0.3440.75-r0 
    && curl https://bootstrap.pypa.io/get-pip.py | python3 
    && adduser -g chromegroup -D chrome 
    && pip3 install -r /tmp/requirements.txt && rm /tmp/requirements.txt
USER chrome
tip:这边还有一个坑,chromechromium都不能在root模式下运行,而且也不安全。所以最好是创建一个用户来运行。

使用docker的时候,run时候需要加--privileged参数

如果你需要了解如何在root用户下运行chrome,请阅读这篇博文
Ubuntu16.04安装Chrome浏览器及解决root不能打开的问题

requirements.txt

Scrapy
selenium
Twisted
PyMysql
pyvirtualdisplay

requirements.txtDockerfile放在一起。
并在目录下使用docker命令docker build -t "chromium-scrapy-image" .

至于为什么要安装xvfbpyvirtualdisplay。因为chromiumheadless模式下不能处理带账号密码的问题。待会就会说到了。

RedhatDebian可以去包仓库找一下最新的chromium和对应的chromium-drive下载安装就可以了。版本一定要是对应的!这边使用chromium=68.0.3440.75-r0chromium-chromedriver=68.0.3440.75-r0


修改ScrapyMiddleware

使用了chromium之后,我们在middlewares.py文件修改一下。我们的设想是让chromium来替代掉request请求。所以我们修改了DownloaderMiddleware

#DownloaderMiddleware
class DemoDownloaderMiddleware(object):
    def __init__(self):
        chrome_options = webdriver.ChromeOptions()
        # 启用headless模式
        chrome_options.add_argument("--headless")
        # 关闭gpu
        chrome_options.add_argument("--disable-gpu")
        # 关闭图像显示
        chrome_options.add_argument("--blink-settings=imagesEnabled=false") 
        self.driver = webdriver.Chrome(chrome_options=chrome_options)
        
    def __del__(self):
        self.driver.quit()
        
    @classmethod
    def from_crawler(cls, crawler):
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s
        
    def process_request(self, request, spider):
        # chromium处理
        # ...
        return HtmlResponse(url=request.url, 
        body=self.driver.page_source, 
        request=request, 
        encoding="utf-8", 
        status=200)
        
    def process_response(self, request, response, spider):
        # Called with the response returned from the downloader.

        # Must either;
        # - return a Response object
        # - return a Request object
        # - or raise IgnoreRequest
        return response

    def process_exception(self, request, exception, spider):
        # Called when a download handler or a process_request()
        # (from other downloader middleware) raises an exception.

        # Must either:
        # - return None: continue processing this exception
        # - return a Response object: stops process_exception() chain
        # - return a Request object: stops process_exception() chain
        pass

    def spider_opened(self, spider):
        spider.logger.info("Spider opened: %s" % spider.name)
tip:这边我们只有一个中间件来处理request。也就是说,所有的逻辑都要经过这儿。所以直接返回了response

这就解决了seleniumchromium的安装问题。

chromium不支持headless问题

如果你安装的chromium版本太老,不支持headless,不着急。之前我们安装的xvfbpyvirtualdisplay就派上用场了。

from pyvirtualdisplay import Display
...
>>>
chrome_options.add_argument("--headless")

<<<
# chrome_options.add_argument("--headless")
display=Display(visible=0,size=(800,800))
display.start()
...

>>>
self.driver.quit()

<<<
self.driver.quit()
display.stop()
...

我们模拟出了一个显示界面,这个时候,不管chromium开不开启headless,都能在我们的服务器上运行了。

代理

因为我们已经用chromium替换了request。所以我们做的代理也不能在Scrapy中来处理。
我们需要直接用chromium来处理IP代理问题。

这是不使用chromium之前使用代理的办法

class DemoProxyMiddleware(object):
    # overwrite process request

    def process_request(self, request, spider):
        # Set the location of the proxy
        request.meta["proxy"] = "https://proxy.com:8080"

        # Use the following lines if your proxy requires authentication
        
        proxy_user_pass = "username:password"
        encoded_user_pass = base64.b64encode(proxy_user_pass.encode("utf-8"))

        # setup basic authentication for the proxy
        request.headers["Proxy-Authorization"] = "Basic " + str(encoded_user_pass, encoding="utf-8")

如果你的IP代理不需要账号密码的话,只需要把后面三行删除了就可以了。

根据上面这段代码,我们也不难猜出chromium解决代理的方法了。

chrome_options.add_argument("--proxy=proxy.com:8080")

只需要加一段argument就可以了。

那解决带账号密码的办法呢?

解决chromium下带账号密码的代理问题

先创建一个py文件

import string
import zipfile


def create_proxyauth_extension(proxy_host, proxy_port,
                               proxy_username, proxy_password,
                               scheme="http", plugin_path=None):
    """代理认证插件

    args:
        proxy_host (str): 你的代理地址或者域名(str类型)
        proxy_port (int): 代理端口号(int类型)
        proxy_username (str):用户名(字符串)
        proxy_password (str): 密码 (字符串)
    kwargs:
        scheme (str): 代理方式 默认http
        plugin_path (str): 扩展的绝对路径

    return str -> plugin_path
    """

    if plugin_path is None:
        plugin_path = "vimm_chrome_proxyauth_plugin.zip"

    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Chrome Proxy",
        "permissions": [
            "proxy",
            "tabs",
            "unlimitedStorage",
            "storage",
            "",
            "webRequest",
            "webRequestBlocking"
        ],
        "background": {
            "scripts": ["background.js"]
        },
        "minimum_chrome_version":"22.0.0"
    }
    """

    background_js = string.Template(
        """
        var config = {
                mode: "fixed_servers",
                rules: {
                  singleProxy: {
                    scheme: "${scheme}",
                    host: "${host}",
                    port: parseInt(${port})
                  },
                  bypassList: ["foobar.com"]
                }
              };
    
        chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
    
        function callbackFn(details) {
            return {
                authCredentials: {
                    username: "${username}",
                    password: "${password}"
                }
            };
        }
    
        chrome.webRequest.onAuthRequired.addListener(
                    callbackFn,
                    {urls: [""]},
                    ["blocking"]
        );
        """
    ).substitute(
        host=proxy_host,
        port=proxy_port,
        username=proxy_username,
        password=proxy_password,
        scheme=scheme,
    )
    with zipfile.ZipFile(plugin_path, "w") as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)

    return plugin_path

使用方式

    proxyauth_plugin_path = create_proxyauth_extension(
        proxy_host="host",
        proxy_port=port,
        proxy_username="user",
        proxy_password="pwd")
    chrome_options.add_extension(proxyauth_plugin_path)

这样就完成了chromium的代理了。但是,如果你开启了headless模式,这个方法会提示错误。所以解决办法就是,关闭headless模式。
至于怎么在没有gui的情况下使用chromium。在之前已经提到过,使用xvfbpyvirtualdisplay就可以了。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/42310.html

相关文章

  • Python入门网络爬虫之精华版

    摘要:学习网络爬虫主要分个大的版块抓取,分析,存储另外,比较常用的爬虫框架,这里最后也详细介绍一下。网络爬虫要做的,简单来说,就是实现浏览器的功能。 Python学习网络爬虫主要分3个大的版块:抓取,分析,存储 另外,比较常用的爬虫框架Scrapy,这里最后也详细介绍一下。 首先列举一下本人总结的相关文章,这些覆盖了入门网络爬虫需要的基本概念和技巧:宁哥的小站-网络爬虫,当我们在浏览器中输入...

    Bmob 评论0 收藏0
  • 首次公开,整理12年积累的博客收藏夹,零距离展示《收藏夹吃灰》系列博客

    摘要:时间永远都过得那么快,一晃从年注册,到现在已经过去了年那些被我藏在收藏夹吃灰的文章,已经太多了,是时候把他们整理一下了。那是因为收藏夹太乱,橡皮擦给设置私密了,不收拾不好看呀。 ...

    Harriet666 评论0 收藏0
  • 网络爬虫介绍

    摘要:什么是爬虫网络爬虫也叫网络蜘蛛,是一种自动化浏览网络的程序,或者说是一种网络机器人。 什么是爬虫 网络爬虫也叫网络蜘蛛,是一种自动化浏览网络的程序,或者说是一种网络机器人。它们被广泛用于互联网搜索引擎或其他类似网站,以获取或更新这些网站的内容和检索方式。它们可以自动采集所有其能够访问到的页面内容,以供搜索引擎做进一步处理(分检整理下载的页面),而使得用户能更快的检索到他们需要的信息。简...

    sf190404 评论0 收藏0
  • 精通Python网络爬虫(0):网络爬虫学习路线

    摘要:以上是如果你想精通网络爬虫的学习研究路线,按照这些步骤学习下去,可以让你的爬虫技术得到非常大的提升。 作者:韦玮 转载请注明出处 随着大数据时代的到来,人们对数据资源的需求越来越多,而爬虫是一种很好的自动采集数据的手段。 那么,如何才能精通Python网络爬虫呢?学习Python网络爬虫的路线应该如何进行呢?在此为大家具体进行介绍。 1、选择一款合适的编程语言 事实上,Python、P...

    spacewander 评论0 收藏0
  • 非计算机专业小白自学爬虫全指南(附资源)

    摘要:爬虫是我接触计算机编程的入门。练练练本文推荐的资源就是以项目练习带动爬虫学习,囊括了大部分爬虫工程师要求的知识点。拓展阅读一文了解爬虫与反爬虫最后,请注意,爬虫的工作机会相对较少。 爬虫是我接触计算机编程的入门。哥当年写第一行代码的时候别提有多痛苦。 本文旨在用一篇文章说透爬虫如何自学可以达到找工作的要求。 爬虫的学习就是跟着实际项目去学,每个项目会涉及到不同的知识点,项目做多了,自然...

    blair 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<