我想通过向下滚动网页生成的内容反向工程。问题出在url https://www.crowdfunder.com/user/following_page/80159?user_id=80159&limit=0&per_page=20&screwrand=933中。screwrand似乎不遵循任何模式,所以反向urls不起作用。我正在考虑使用Splash进行自动渲染。如何使用Splash来像浏览器一样滚动?非常感谢!以下是两个请求的代码:
request1 = scrapy_splash.SplashRequest(
'https://www.crowdfunder.com/user/following/{}'.format(user_id),
self.parse_follow_relationship,
args={'wait':2},
meta={'user_id':user_id, 'action':'following'},
endpoint='http://192.168.99.100:8050/render.html')
yield request1
request2 = scrapy_splash.SplashRequest(
'https://www.crowdfunder.com/user/following_user/80159?user_id=80159&limit=0&per_page=20&screwrand=76',
self.parse_tmp,
meta={'user_id':user_id, 'action':'following'},
endpoint='http://192.168.99.100:8050/render.html')
yield request2发布于 2016-11-02 02:36:04
要滚动页面,您可以编写一个自定义呈现脚本(请参阅http://splash.readthedocs.io/en/stable/scripting-tutorial.html),如下所示:
function main(splash)
local num_scrolls = 10
local scroll_delay = 1.0
local scroll_to = splash:jsfunc("window.scrollTo")
local get_body_height = splash:jsfunc(
"function() {return document.body.scrollHeight;}"
)
assert(splash:go(splash.args.url))
splash:wait(splash.args.wait)
for _ = 1, num_scrolls do
scroll_to(0, get_body_height())
splash:wait(scroll_delay)
end
return splash:html()
end要呈现此脚本,请使用'execute‘端点,而不是render.html端点:
script = """<Lua script> """
scrapy_splash.SplashRequest(url, self.parse,
endpoint='execute',
args={'wait':2, 'lua_source': script}, ...)发布于 2018-10-30 10:26:26
谢谢Mikhail,我试过你的滚动脚本,它工作了,但我也注意到你的脚本滚动太多一次,一些js没有时间渲染而被跳过,所以我做了一些小的修改,如下所示:
function main(splash)
local num_scrolls = 10
local scroll_delay = 1
local scroll_to = splash:jsfunc("window.scrollTo")
local get_body_height = splash:jsfunc(
"function() {return document.body.scrollHeight;}"
)
assert(splash:go(splash.args.url))
splash:wait(splash.args.wait)
for _ = 1, num_scrolls do
local height = get_body_height()
for i = 1, 10 do
scroll_to(0, height * i/10)
splash:wait(scroll_delay/10)
end
end
return splash:html()
endhttps://stackoverflow.com/questions/40325657
复制相似问题