gpt4 book ai didi

python - Scrapy with Splash 不会等待网站加载

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:37 41 4
gpt4 key购买 nike

我正在尝试通过 Python 脚本调用 Splash 来呈现和抓取交互式网站,基本上遵循此 tutorial :

import scrapy
from scrapy_splash import SplashRequest

class MySpider(scrapy.Spider):
start_urls = ["http://example.com"]

def start_requests(self):
for url in self.start_urls:
yield SplashRequest(url, self.parse,
endpoint='render.html',
args={'wait': 0.5},
)

def parse(self, response):
filename = 'mywebsite-%s.html' % '1'
with open(filename, 'wb') as f:
f.write(response.body)

输出看起来不错,但是它缺少在一两秒后通过 ajax 加载的网站的一部分,这是我真正需要的内容。现在奇怪的是,如果我通过 Web 界面直接在容器内访问 Splash,设置相同的 URL,然后点击 Render 按钮,返回的响应是正确的。所以,唯一的问题是,为什么当 Python 脚本调用它时,它没有正确呈现网站?

最佳答案

我已经尝试了 adrihanu 的建议,但没有奏效。过了一会儿,我想知道会发生什么,以及是否有可能执行 Splash UI 正在执行的相同脚本。所以,我了解到可以将 lua 脚本作为参数传递,而且它有效!

script1 = """
function main(splash, args)
assert (splash:go(args.url))
assert (splash:wait(0.5))
return {
html = splash: html(),
png = splash:png(),
har = splash:har(),
}
end
"""

def start_requests(self):
for url in self.start_urls:
yield SplashRequest(url, self.parse,
endpoint='execute',
args={
'html': 1,
'lua_source': self.script1,
'wait': 0.5,
}

关于python - Scrapy with Splash 不会等待网站加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51803101/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com