我是新来的Python,我甚至不知道我是否问得很对,但事情是我需要发送一个请求到一个网站登录,这个网站正在给我回复一条消息,告诉我我必须在使用该网站之前启用JavaScript。如果没有Selenium或类似软件,我可以绕过“Javascript is required”吗?
我用硒做了,一切正常,工作正常(更多的东西,不仅仅是登录),但现在我想让它没有Selenium,实际上没有任何浏览器窗口,这甚至有可能吗?我想这是,但是,我需要一些帮助,因为我没有找到办法。
#!/usr/bin/python3
import requests
userEmail = "[email protected]" #using real data in the script
userPass = "xxxxxxxxxxxxx" #using real data in the script
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest'
}
def main():
r = requests.post('https://www.thedomain.com/en/customer/account/loginPost/',
data = {'login[username]':userEmail, "login[password]":userPass}, headers=headers)
print(r.text)
if __name__ == "__main__": main()
,这是我的消息得到:
<html>
<title>You are being redirected...</title>
<noscript>Javascript is required. Please enable javascript before you are allowed to see this page.</noscript>
</html>
我可以绕过这个无硒?
我会说不。页面可能是动态生成的(例如使用Angular或React),所以如果JS被禁用,您就不可能看到生成的内容。 –
但是,如果您希望在不使用浏览器窗口的情况下执行此操作,请查看phantomjs,您仍然使用硒但它是一个“无头”浏览器,因此不会出现窗口,但它仍然可以运行所需的javascript –
重要的是你得到什么回应?登录逻辑如何工作?通常,在某处登录最终会得到一个cookie或类似的东西(包含会话ID),您将在随后的所有请求中使用它来表明您是刚刚登录的用户。 – Metareven