I cannot autologin to pastebin using requests + BeautifulSoup

与世无争的帅哥 提交于 2021-01-29 20:53:39

问题


I am trying to auto-login to pastebin account using python, but im failing and i don't know why. I copied the request headers exactly and double checked... but still i am greeted with 400 HTTP code. Can somebody help me?

This is my code:

import requests
from bs4 import BeautifulSoup
import subprocess
import os
import sys
from requests import Session


# the actual program
page = requests.get("https://pastebin.com/99qQTecB")
parse = BeautifulSoup(page.content, 'html.parser')
string = parse.find("textarea")
command = 'hello'
###########################################################
URL = 'https://pastebin.com'
LOGIN_ROUTE ='/login'

d = requests.session()
cfduid = d.get(URL).cookies['__cfduid']

e = requests.session()
csrf = e.get(URL).cookies['_csrf-frontend']

f = requests.session()
pastebin = f.get(URL).cookies['pastebin-frontend']


print (csrf)
print(cfduid)
print(pastebin)

HEADERS = {'Host':'pastebin.com', 'User-Agent':'Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language':'en-US,en;q=0.5',
'Accept-Encoding':'gzip, deflate, br' , 'Referer': 'https://pastebin.com/login', 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': '174', 'Connection': 'keep-alive', 'Cookie': "__cfduid=" + cfduid + ";", '_csrf-frontend':csrf + ";"
,'pastebin-frontend':pastebin + ";" ,'Upgrade-Insecure-Requests': '1'}

if command in string:
    /super_mario_bros.exe', shell=True)
    s = requests.session()
    csrf_token = s.get(URL).cookies['_csrf-frontend']
    
    login_payload = {
        'LoginForm[username]': 'SECRET',
        'LoginForm[password]': 'Secret', 
        '_csrf-frontend': csrf_token
        }
        #Actual Login
    login_req = s.post(URL + LOGIN_ROUTE, data=login_payload)
    print(csrf_token)
    print(login_req.status_code)    
    
    
else:
    print("smth")


#print(string)



#Cookie: __cfduid=d955068eb3316226875aa037c059fd8f11595841495; __gads=ID=49ab9fcccba85989:T=1595841504:S=ALNI_MYT-PJZzkGrbYunHFHQE-EEw3vfhQ; _ga=GA1.2.1839432134.1595810341; pastebin-frontend=a9bf2e8c462237148c2d5f6b0832387c; _csrf-frontend=8213fdd4c42f9cfad45ed4993a25a1753c3539b1700c9e92be8ffac00780e34ea%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22HHm8aNkQL8XAG8joV8KfCZqOTls50yyW%22%3B%7D; _gid=GA1.2.1476917794.1596212111

I am really exhausted cause for the last couple of hours ive been trying to make this work, but still 400 Code is there. Thank you in advance!


回答1:


I can recommend browser_cookie3:

import browser_cookie3
import requests
from bs4 import BeautifulSoup
cj = browser_cookie3.load()
s = requests.Session()
for c in cj:
    if 'pastebin' in str(c):
        s.cookies.set_cookie(c)
r = s.get('https://pastebin.com/')
soup = BeautifulSoup(r.content, 'lxml')
print(soup.select_one('div.header__user-name').text) 

prints:

UWTD

This code take the session cookies from the browser and use them. And prints the username of the user.



来源:https://stackoverflow.com/questions/63195770/i-cannot-autologin-to-pastebin-using-requests-beautifulsoup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!