使用Python requests_html执行JavaScript函数

发布于 2025-02-11 16:49:47 字数 1249 浏览 1 评论 0原文

我正在尝试使用Python的requests_html在会话中执行JavaScript函数(单击按钮单击),

我了解常规requests library没有Javascript支持,所以我正在尝试改用request_html

这是我所拥有的(使用请求):

s = requests.Session()
r = s.post(url)
print(r.text)
r2 = s.post(url2)
print(r2.text)

url是包含按钮的页面的链接,url2是帖子请求链接链接link the按钮的JavaScript函数执行。 (我在浏览器检查器中通过“网络”选项卡找到了url2,并单击按钮作为测试)

但是,这是行不通的,我从r2.Text

<h2>Error(500): An error occurred.</h2>
                <p>We are sorry but an unexpected error has occurred on our side while handling your request. In the meantime, please retry your request or try the following:</p>

据我了解,错误500意味着问题是服务器端,而不是客户端。但是,在网页上手动单击按钮可以正常工作。

这使我试图直接执行JavaScript函数。我在requests_html文档上找不到任何东西。我也看过硒,但这似乎并非最新。

还值得一提的是,按钮检查器看起来像这样:&lt; button onClick =“ registerInterest(72833,959320000,'')“ type =“ button” class =“ btn btn btn btn-primary”&gt; class =“ far fa-clipboard”&gt;&lt;/i&gt;注册利息&lt;/button&gt;

本质上,我想执行registerInterest(72833,959320000,''')在我的第一个发布请求之后。

任何帮助将不胜感激, 我将很乐意提供任何其他需要的信息。

I am trying to execute a JavaScript function (run with a button click) within a session using Python's requests_html

I understand the regular requests library does not have JavaScript support so I am trying to use requests_html instead.

Here's what I have (using requests):

s = requests.Session()
r = s.post(url)
print(r.text)
r2 = s.post(url2)
print(r2.text)

url is the link to the page containing the button and url2 is the POST request link the button's JavaScript function executes. (I found url2 through the network tab while in my browser inspector and clicking the button as a test)

However, this does not work and I get this from r2.text:

<h2>Error(500): An error occurred.</h2>
                <p>We are sorry but an unexpected error has occurred on our side while handling your request. In the meantime, please retry your request or try the following:</p>

To my understanding, an error 500 means that the issue is server-side, not client-side. However, clicking the button manually on the webpage works fine.

This brings me to attempting to directly execute the JavaScript function instead. I couldn't find anything on the requests_html documentation. I've also looked at Selenium, but that doesn't seem to be up to date.

It is also worth mentioning that the button inspector looks like this: <button onclick="registerInterest(72833,959320000, '')" type="button" class="btn btn-primary"><i class="far fa-clipboard"></i> Register Interest</button>

So essentially, I would like to execute registerInterest(72833,959320000, '') after my first POST request.

Any help would be greatly appreciated,
I will gladly provide any additional needed information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对风讲故事 2025-02-18 16:49:47

您需要使用硒来操纵HTML元素。您可以使用这样的代码:

from selenium import webdriver
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
#implicit wait
driver.implicitly_wait(0.5)
#maximize browser
driver.maximize_window()
#launch URL
driver.get("https://www.tutorialspoint.com/index.htm")
#identify element
l =driver.find_element_by_xpath("//button[text()='Check it Now']")
#perform click
l.click()
print("Page title is: ")
print(driver.title)
#close browser
driver.quit()

只需检查有关硒方法的文档,然后找到适合您最好的方法。

You need to use Selenium for manipulating html elements. You can use code like this:

from selenium import webdriver
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
#implicit wait
driver.implicitly_wait(0.5)
#maximize browser
driver.maximize_window()
#launch URL
driver.get("https://www.tutorialspoint.com/index.htm")
#identify element
l =driver.find_element_by_xpath("//button[text()='Check it Now']")
#perform click
l.click()
print("Page title is: ")
print(driver.title)
#close browser
driver.quit()

Just check docs on methods of Selenium and find a method which fits you the best.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文