python - 使用 scrapy-splash 点击按钮

标签 python scrapy

我正在尝试使用 Scrapy-splash单击页面上的按钮 I'm being redirected to .

我已经测试了手动点击页面,在我点击表示同意的按钮后我被重定向到正确的页面。当我被重定向到页面时,我编写了一个小脚本来单击按钮,但这不起作用。

我在下面包含了我的蜘蛛代码片段 - 我的代码中是否遗漏了什么?

from sys import path
import os

dir_path = os.path.dirname(os.path.realpath(__file__))
path.append(dir_path)

import scrapy
from scrapy_splash import SplashRequest

script="""
function main(splash)
    splash:wait(1)
    splash:runjs('document.querySelector("form.consent-form").submit()')
    splash:wait(1)
    return {
        html = splash:html(),
    }
end
"""


class FoobarSpider(scrapy.Spider):
    name = "foobar"          

    def start_requests(self):
        urls = ['https://uk.finance.yahoo.com/quote/ANTO.L?p=ANTO.L']

        for url in urls:
            yield SplashRequest(url=url, callback=self.parse,
                    endpoint='render.html',
                    args={'wait': 3},
                    meta = {'yahoo_url': url }
                )



    def parse(self, response):
        url = response.url

        with open('temp.html', 'wb') as f:
            f.write(response.body)

        if 'https://guce.' in url:
            print('About to attempt to authenticate ...')
            yield SplashRequest(
                                    url, 
                                    callback = self.get_price, 
                                    endpoint = 'execute',
                                    args = {'lua_source': script, 'timeout': 5},
                                    meta = response.meta 
                                )

        else:
            self.get_price(response)




    def get_price(self, response):    
        print("Get price called!")
        yahoo_price = None          

        try:
            # Get Price ...
            temp1 = response.css('div.D\(ib\).Mend\(20px\)')
            if temp1 and len(temp1) > 1:
                temp2 = temp1[1].css('span')
                if len(temp2) > 0:
                    yahoo_price = temp2[0].xpath('.//text()').extract_first().replace(',','') 

            if not yahoo_price:
                val = response.css('span.Trsdu\(0\.3s\).Trsdu\(0\.3s\).Fw\(b\).Fz\(36px\).Mb\(-4px\).D\(b\)').xpath('.//text()').extract_first().replace(',','')
                yahoo_price = val


        except Exception as err:
            pass           


        print("Price is: {0}".format(yahoo_price))


    def handle_error(self, failure):
        pass

我该如何解决这个问题,以便我可以正确地表示同意,从而将我定向到我想要的页面?

最佳答案

与其点击按钮,不如尝试提交表单:

document.querySelector("form.consent-form").submit()

我尝试在我的控制台中运行 JavaScript 命令 input.btn.btn-primary.agree").click() 并且会收到错误消息“糟糕,出了点问题”但是页面使用上述代码提交表单时加载。

因为我不在欧洲,所以无法完全重新创建您的设置,但我相信这应该可以帮助您解决问题。我的猜测是这个 script正在干扰 .click() 方法。

关于python - 使用 scrapy-splash 点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51085067/

相关文章:

python - 根据字符串中的特定逗号有选择地将字符串拆分为元组列表

python - scrapy-splash 事件内容选择器在 shell 中工作,但不适用于蜘蛛

mongodb - pymongo.errors.ConnectionFailure : timed out from an ubuntu ec2 instance running scrapyd

python - 蜘蛛的 Scrapy 域名

python - 使用 python 计算文本文档的逐点互信息

python - 解析Excel报告

python - 使用 Scrapy 匹配 robots.txt 中的多个用户代理

python - Scrapy Spider 在同一页面获取两张图片,然后以不同的方式命名它们

python ctypes 挂

python - uWSGI找不到所有Python安装中都存在的模块