我正在尝试使用 pythons requests 库登录 slicethepie.com。
根据我的理解,我收到 419 是因为我没有传递 csrf token 。我在 Chrome 网络选项卡中看到的唯一类似 token 是 XSRF-TOKEN。 我需要通过它吗?为什么我的代码返回 419?
您当前可以看到我正在 cookie 中传递 XSRF token ,但运气不佳。
import requests
link = "https://www.slicethepie.com/login"
response = requests.get(link) # no XSRF token in here
payload = { "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="543a3123203538313a20646d646d143339353d387a373b39" rel="noreferrer noopener nofollow">[email protected]</a>", "password": "password", "_token": "w4uj7bk4KRjheHk05MajnrWfHyS9PmN7mmMgUpx5", "validation": "token", location[latitude]: None, location[longitude]: None}
headers = {
"Cookie": "XSRF-TOKEN=...tokenInImage...slicethepie_session=...tokenInImage...",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
"Referer": "https://www.slicethepie.com/login",
"Origin": "https://www.slicethepie.com",
}
with requests.Session() as s:
p = s.post(
"https://www.slicethepie.com/login",
headers=headers,
json=payload,
)
print(p) # returns 419. Page Expired
有效负载中的表单数据
_token: w4uj7bk4KRjheHk05MajnrWfHyS9PmN7mmMgUpx5
_token: w4uj7bk4KRjheHk05MajnrWfHyS9PmN7mmMgUpx5
validation: token
location[latitude]: None
location[longitude]: None
email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2749425053464b424953171e171e67404a464e4b0944484a" rel="noreferrer noopener nofollow">[email protected]</a>
password: password
最佳答案
以下代码经过测试并且工作正常:
import requests
from bs4 import BeautifulSoup as bs
import re
link = "https://www.slicethepie.com/login"
s = requests.Session()
r = s.get(link) # no XSRF token in here
token_value = bs(r.text, 'html.parser').select_one('form input[name="_token"]').get('value')
payload = { "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f9979c8e8d98959c978dc9c0c9c0b99e94989095d79a9694" rel="noreferrer noopener nofollow">[email protected]</a>", "password": "password", "_token": token_value, 'location[latitude]': None, 'location[longitude]': None}
headers = {
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
"Referer": "https://www.slicethepie.com/login",
"Origin": "https://www.slicethepie.com"
}
s.headers.update(headers)
p = s.post("https://www.slicethepie.com/login",data=payload)
print(p)
print('_________________________________________')
print(bs(p.text, 'html.parser').text)
终端结果:
<Response [200]>
_________________________________________
What would you like to review? on local
- Slice the pie
My account
Profile
Balance
Reviews
Email preferences
Change password
Close account
Notifications
View older
Your balance is $0.00
Lifetime totals
0
reviews
0
friends
totals
Make a withdrawal
View transaction history
Star rating
This is a measure of the quality of your reviews, the more Stars you have the more you'll earn!
Review
Refer a friend
$0.00
Privacy policy
FAQ
Terms and conditions
Contact us
About us
Log out
What would you like to review?
Select a category
Unfortunately we are unable to accept reviews from your location at this time. We will email you when the site becomes available to you.
FAQ
Privacy policy
Terms and conditions
About us
© Shisho Data ltd 2023
关于python - 使用 post 请求和 xsrf token 登录,但收到 419,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76824805/