Python请求后混淆

2024-09-24 10:18:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我的头撞到墙上了…有谁能帮我把脑筋集中在申请一份工作上吗?在

我试图在一个网站上工作,但首先我必须通过登录。。。我一直在谷歌上搜索,但到目前为止我还没弄清楚。我对网络很在行,但在玛雅的python方面有一些经验。在

这是我到目前为止的情况

import requests
login_url = 'https://example.com/examplelogin
login_payload = {'j_username':'myusername', 'j_password':'mypassword'}
with requests.Session() as s:
    r = s.post(login_url, data = login_payload)

这是我要填写的表格:

^{pr2}$

对于r.status_code我得到200,但是r.text只是登录页面HTML。。。它不能让我登录并让我通过。。。在

我错过了什么?在


Tags: httpsimport网络comurl网站exampleusername
3条回答

如果站点没有某种API,则可能无法使用请求库来访问它。你应该看看Selenium它允许你使用FireFox访问页面。如果你想要一个没有头的版本,通常要快得多,你可以使用phantomjs。在

from selenium.webdriver.firefox.webdriver import WebDriver


driver = WebDriver()
driver.get('https://example.com/examplelogin')

username = driver.find_element_by_id('username')
password = driver.find_element_by_id('password')

username.send_keys('MY_USERNAME')
password.send_keys('MY_PASSWORD')

submit.click()

您希望将请求发送到/idp/Authn/UserPassword而不是/examplelogin。如果表单有action属性,浏览器将把POST发送到该属性中指定的地址。在

但是,如果您想使用site,那么最好按照@emett speer的建议使用Selenium,因为您可能不想手动与站点对话(如果URL将被更改,会怎么样等等)。在

请求是一个HTTP库。在使用它之前,你必须阅读关于HTTP的内容。在

HTTP functions as a request-response protocol in the client-server computing model. A web browser, for example, may be the client and an application running on a computer hosting a web site may be the server. The client submits an HTTP request message to the server. The server, which provides resources such as HTML files and other content, or performs other functions on behalf of the client, returns a response message to the client. The response contains completion status information about the request and may also contain requested content in its message body.

事实上,你的表单应该和POST请求一样。你还得读一读forms。在

我认为完成一个教程来了解它是如何工作的是一个好主意。There is a good place to start.

相关问题 更多 >