Scrapy Splash无法执行lua脚本

Rob*_*her 4 scrapy scrapy-splash splash-js-render

我遇到了我的Lua脚本拒绝执行的问题。从ScrapyRequest调用返回的响应似乎是HTML正文,而我希望获得文档标题。我假设从未调用过Lua脚本,因为它似乎对响应没有明显影响。我已经在文档中进行了很多研究,似乎还无法弄清楚这里缺少什么。有没有人有什么建议?

from urlparse import urljoin

import scrapy
from scrapy_splash import SplashRequest


GOOGLE_BASE_URL = 'https://www.google.com/'
GOOGLE_QUERY_PARAMETERS = '#q={query}'
GOOGLE_SEARCH_URL = urljoin(GOOGLE_BASE_URL, GOOGLE_QUERY_PARAMETERS)

GOOGLE_SEARCH_QUERY = 'example search query'


LUA_SCRIPT = """
function main(splash)
    assert(splash:go(splash.args.url))
    return splash:evaljs("document.title")
end
"""

SCRAPY_CRAWLER_NAME = 'google_crawler'
SCRAPY_SPLASH_ENDPOINT = 'render.html'
SCRAPY_ARGS = {
    'lua_source': LUA_SCRIPT
}


def get_search_url(query):
    return GOOGLE_SEARCH_URL.format(query=query)


class GoogleCrawler(scrapy.Spider):
    name=SCRAPY_CRAWLER_NAME
    search_url = get_search_url(GOOGLE_SEARCH_QUERY)

    def start_requests(self):

        response = SplashRequest(self.search_url,
            self.parse, endpoint=SPLASH_ENDPOINT, args=SCRAPY_ARGS)

        yield response


    def parse(self, response):
        doc_title = response.body_as_unicode()
        print doc_title
Run Code Online (Sandbox Code Playgroud)

Mik*_*bov 8

为了执行Lua脚本,SplashRequest的'endpoint'参数必须为'execute'。在示例中为“ render.html”。