简体   繁体   English

Python请求-在同一页面上发送两个请求?

[英]Python requests - Send two requests on the same page?

I am trying to change the username of an account on a website. 我正在尝试更改网站上帐户的用户名。 There are 3 POST parameters required: 需要3个POST参数:

1) newName 1)newName
2) password 2)密码
3) authenticityToken 3)authenticityToken

The authenticityToken is randomly generated each time the page is loaded. 每次加载页面时都会随机生成authenticityToken。 It is input type hidden: 它是隐藏的输入类型:

<input type="hidden" name="authenticityToken" value="0640ce533fc7a51b88f79ce17cdbd611f4dc2360"/>

What I am trying to achieve is to get the value of the authenticityToken and on the same page POST the 3 parameters required as stated above. 我正在尝试实现的是获取authenticityToken的值,并在同一页面上发布上述所需的3个参数。 At the moment, my code receives an authenticityToken using a GET request, and then sends another POST request. 目前,我的代码使用GET请求接收一个authenticityToken,然后发送另一个POST请求。 It returns 它返回

Bad authenticity token 错误的真实性令牌

The code I am currently using is below: 我当前使用的代码如下:

import requests
from bs4 import BeautifulSoup

url = "https://account.mojang.com"

#Login and save cookies
r = requests.post("https://account.mojang.com/login", data={'username':'theUsername','password':'thePassword','remember':'true'})
Logincookies = r.cookies

#Parse the page to get rename link
soup = BeautifulSoup(r.text, "html.parser")
renamePage = None
sources = soup.findAll('a',{"href":True})
for source in sources:
    if("/me/renameProfile/" in source["href"]):
       renamePage = source["href"]


#Get AuthToken

authToken = None
r = requests.get(url + renamePage,cookies=Logincookies)
parser = BeautifulSoup(r.text,"html.parser")
srcs = parser.findAll('input')
for src in srcs:
    if(src["name"] == "authenticityToken"):
        authToken = src["value"]
print(authToken)


r = requests.post(url + renamePage, cookies=Logincookies, data={'newName':'theNewUsername','password':'thePassword','authenticityToken':authToken})
print(r.text)

Once again, how can I do this so that the GET and POST request go to together so that it does not use a new page and receive a new authenticityToken? 再一次,我该如何做才能使GET和POST请求一起执行,以便它不使用新页面并接收新的authenticityToken?

Try to use Session . 尝试使用Session It should allow you to use same cookies , access tokens etc in different requests: 它应该允许您在不同的请求中使用相同的cookies ,访问令牌等:

s = requests.Session()
url = "https://account.mojang.com"
r = s.post("https://account.mojang.com/login", data={'username':'theUsername','password':'thePassword','remember':'true'})
Logincookies = r.cookies
...
r = s.get(url + renamePage,cookies=Logincookies)

PS In some cases you might also need to specify client in headers , eg headers={ "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0" } PS在某些情况下,您可能还需要在headers指定客户端,例如headers={ "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0" }

So the solution was to use a library called RoboBrowser and feed it the same session used as my earlier requests: 因此,解决方案是使用一个名为RoboBrowser的库,并将其提供给与我之前的请求相同的会话:

import requests
from bs4 import BeautifulSoup
from robobrowser import RoboBrowser

session=requests.session()
r = session.post(...)
r = session.get("...")

browser.open(url+renamePage)
rename_form = browser.get_form(class_='standard')
rename_form['newName'].value = "theNewUser"
rename_form['password'].value = "theNewPass"

browser.submit_form(rename_form)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM