使用Python requests_html执行JavaScript函数
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用硒来操纵HTML元素。您可以使用这样的代码:
只需检查有关硒方法的文档,然后找到适合您最好的方法。
You need to use Selenium for manipulating html elements. You can use code like this:
Just check docs on methods of Selenium and find a method which fits you the best.