【发布时间】:2020-01-04 02:11:41
【问题描述】:
我是 scrapy 和 splash 的新手,我需要从单页和常规网络应用中抓取数据。
但需要注意的是,我主要从内部工具和应用程序中抓取数据,因此有些需要身份验证,并且所有这些都需要至少几秒钟的加载时间才能完全加载页面。
我天真地尝试了 Python time.sleep(seconds),但没有成功。基本上,SplashRequest 和 scrapy.Request 似乎都运行并产生结果。然后,我了解了将 LUA 脚本作为这些请求的参数,并尝试了具有各种形式的 wait() 的 LUA 脚本,但看起来这些请求从未真正运行过 LUA 脚本。它立即完成,我的 HTMl 选择器没有找到我正在寻找的任何东西。
我从这里 https://github.com/scrapy-plugins/scrapy-splash 开始遵循指示,并让他们的 docker 实例在 localhost:8050 上运行并创建了一个 settings.py。
这里有经验的人知道我可能缺少什么吗?
谢谢!
spider.py
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy_splash import SplashRequest
import logging
import base64
import time
# from selenium import webdriver
# lua_script="""
# function main(splash)
# splash:set_user_agent(splash.args.ua)
# assert(splash:go(splash.args.url))
# splash:wait(5)
# -- requires Splash 2.3
# -- while not splash:select('#user-form') do
# -- splash:wait(5)
# -- end
# repeat
# splash:wait(5))
# until( splash:select('#user-form') ~= nil )
# return {html=splash:html()}
# end
# """
load_page_script="""
function main(splash)
splash:set_user_agent(splash.args.ua)
assert(splash:go(splash.args.url))
splash:wait(5)
function wait_for(splash, condition)
while not condition() do
splash:wait(0.5)
end
end
local result, error = splash:wait_for_resume([[
function main(splash) {
setTimeout(function () {
splash.resume();
}, 5000);
}
]])
wait_for(splash, function()
return splash:evaljs("document.querySelector('#user-form') != null")
end)
-- repeat
-- splash:wait(5))
-- until( splash:select('#user-form') ~= nil )
return {html=splash:html()}
end
"""
class HelpSpider(scrapy.Spider):
name = "help"
allowed_domains = ["secet_internal_url.com"]
start_urls = ['https://secet_internal_url.com']
# http_user = 'splash-user'
# http_pass = 'splash-password'
def start_requests(self):
logger = logging.getLogger()
login_page = 'https://secet_internal_url.com/#/auth'
splash_args = {
'html': 1,
'png': 1,
'width': 600,
'render_all': 1,
'lua_source': load_page_script
}
#splash_args = {
# 'html': 1,
# 'png': 1,
# 'width': 600,
# 'render_all': 1,
# 'lua_source': lua_script
#}
yield SplashRequest(login_page, self.parse, endpoint='execute', magic_response=True, args=splash_args)
def parse(self, response):
# time.sleep(10)
logger = logging.getLogger()
html = response._body.decode("utf-8")
# Looking for a form with the ID 'user-form'
form = response.css('#user-form')
logger.info("####################")
logger.info(form)
logger.info("####################")
【问题讨论】:
标签: python lua scrapy scrapy-splash