javascript - Python-splash:使用 JS 下一步按钮抓取网站
问题描述
我的目标是单击页面上的“下一步按钮”。主要问题 - 下一个按钮没有 url,只有 javascript。
有人可以告诉我解决我的问题的正确方法:
import scrapy
from scrapy_splash import SplashRequest
class QuotesSpider(scrapy.Spider):
name = "cruises"
start_urls = ["https://www.msccruises.com/en-gl/Plan-Book/Find-Cruise.aspx"]
def start_requests(self):
for url in self.start_urls:
yield SplashRequest(url, self.parse, args={'wait': 0.5})
def parse(self, response):
lua_src = """
function main(splash)
splash:go(splash.args.url)
splash:wait(1)
--splash:runjs("document.querySelectorAll('#mainContent_fycResults_rptPagination_btnPageNext')[0].click()")
splash:runjs("document.getElementsByClassName('btnPageNext')[0].click()")
splash:wait(1)
page = splash:html()
return page
end"""
next_page = response.xpath('//a[@class="btnPageNext"]')
if next_page is not None:
url = "https://www.msccruises.com/en-gl/Plan-Book/Find-Cruise.aspx"
yield SplashRequest(
url,
self.parse,
endpoint='execute',
method='POST',
dont_filter=True,
args={
'wait': 1.0,
'lua_source': lua_src,
},
)
解决方案
推荐阅读
- html - 如何更改 RMarkdown HTML 文档中的整个背景颜色?
- python - threading.Timer 协程的替代方案
- java - 使用线程运行的后台应用程序
- linux - 仅在屏幕外重新加载 ffmpeg 中的滚动绘图文本过滤器
- css - 如何将背景图片放在右下角?
- javascript - 如何初始化共享的 javascript 模块默认导出
- python - Python Scrapy在关闭时再次启动Spider
- html - 导航栏中的元素如何将您引导到与徽标在同一行的不同页面?
- asp.net-core - 在通过 window.open 请求的后端页面中识别经过身份验证的用户
- python - 在 python 中,使用 Seaborn 库,有什么方法可以为我的图表值添加百分比刻度?