python - 来自官方 Github 的 ScrapyJS 示例未运行

标签 python web-scraping scrapy

我正在尝试编写一个获取 javascript 代码的小型网络解析器。为此,我尝试使用 ScrapyJS 通过 Javscript 扩展 Scrapy。

我已按照 the official repository 上的安装说明进行操作. Scrapy 本身工作正常,但来自 scrapyJS 的第二个示例(获取 HTML 内容和屏幕截图:)却不行。所以希望我的问题能帮助其他遇到同样问题的人 ;)

我的设置和代码如下(如果需要的话):

  1. 首先,我通过 sudo -H pip install scrapyjs
  2. 安装了 scrapyJS
  3. 然后,我运行以下命令:sudo docker run -p 5023:5023 -p 8050:8050 -p 8051:8051 scrapinghub/splash
  4. 之前,我更改了我的 scrapy 项目的 settings.py。我添加了以下几行:

    DOWNLOADER_MIDDLEWARES = { 'scrapyjs.SplashMiddleware': 725, } DUPEFILTER_CLASS = 'scrapyjs.SplashAwareDupeFilter' HTTPCACHE_STORAGE = 'scrapyjs.SplashAwareFSCacheStorage'

  5. 完整的 python 代码如下所示:

:

import json
import base64
import scrapy
class MySpider(scrapy.Spider):
    name = "dmoz"
    allowed_domains = ["dmoz.org"]
    start_urls = [
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"
    ]

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url, self.parse_result, meta={
            'splash': {
                'args': {
                    'html': 1,
                    'png': 1,
                    'width': 600,
                    'render_all': 1,
                }
            }
        })

    def parse_result(self, response):
        data = json.loads(response.body_as_unicode())
        body = data['html']
        png_bytes = base64.b64decode(data['png'])
        print body

我收到以下错误:

2016-01-07 14:08:16 [scrapy] INFO: Enabled item pipelines: 
2016-01-07 14:08:16 [scrapy] INFO: Spider opened
2016-01-07 14:08:16 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-01-07 14:08:16 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-01-07 14:08:16 [scrapy] DEBUG: Retrying <POST http://127.0.0.1:8050/render.json> (failed 1 times): 400 Bad Request
2016-01-07 14:08:16 [scrapy] DEBUG: Retrying <POST http://127.0.0.1:8050/render.json> (failed 2 times): 400 Bad Request
2016-01-07 14:08:16 [scrapy] DEBUG: Gave up retrying <POST http://127.0.0.1:8050/render.json> (failed 3 times): 400 Bad Request
2016-01-07 14:08:16 [scrapy] DEBUG: Crawled (400) <POST http://127.0.0.1:8050/render.json> (referer: None)
2016-01-07 14:08:16 [scrapy] DEBUG: Ignoring response <400 http://127.0.0.1:8050/render.json>: HTTP status code is not handled or not allowed
2016-01-07 14:08:16 [scrapy] INFO: Closing spider (finished)

所以其实我也不知道错误在哪里。 Scrapy 单独工作。 如果我添加 SPLASH_URL = 'http://192.168.59.103:8050',我将收到超时错误。那时什么都没有发生。 Localhost:8050 都不起作用。将 SPLASH_URL 留空可以解决错误,但随后我会收到上述错误。

最佳答案

您需要传递非零“等待”才能呈现完整网页。

所以只要添加 'wait': 0.5 就可以了。

def start_requests(self):
    for url in self.start_urls:
        yield scrapy.Request(url, self.parse_result, meta={
        'splash': {
            'args': {
                'html': 1,
                'png': 1,
                'width': 600,
                'render_all': 1,
                'wait': 0.5
            }
        }
    })

关于python - 来自官方 Github 的 ScrapyJS 示例未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34656424/

相关文章:

python - 子进程错误文件

python - 如何在Python中打印集合的最小值?

python - 在将字符串强制转换为 unicode 时如何让 python 2.x 发出警告?

python - 如何使用 python pandas 和 BeautifulSoup 在电影中阅读 2018 年的维基百科表格

python - 在本地 HTML 文件上使用 scrapy 内置选择器

python - 在 python for 循环中一次运行 3 个变量。

web-scraping - 在 Nixos 上使用 systemd 计时器运行脚本

python - 用 Selenium python 刮取产品颜色

python - Scrapy 给出 URLError : <urlopen error timed out>

xpath - 遵循所有 p 标签,除非并在出现其他类型的同级时停止