请帮助将Powershell web请求转换为Python

2024-04-26 09:31:20 发布

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

我正在创建一个python机器人,它将每月自动连接到该站点,我需要登录到该帐户。我分析了GoogleChrome的请求,并试图提出一个不是来自浏览器的请求。我试图用Python中的curl、powershell、urllib、request lib发出请求。只有powershell给了我成功的回复。我尝试了许多不同的变体来用python发出请求。但不幸的是,他们中没有一个人没有回复我登录成功。另外,我不擅长http请求等。希望有人能帮助我,谢谢

这是Powershell请求

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"
$session.Cookies.Add((New-Object System.Net.Cookie("_csrf", "jmHLUa6vl-2kL2rN1ba5kRwMw7ICD7mG", "/", "ttv.run")))
Invoke-WebRequest -UseBasicParsing -Uri "http://ttv.run/auth.php" `
-Method "POST" `
-WebSession $session `
-Headers @{
"Cache-Control"="max-age=0"
  "Origin"="http://ttv.run"
  "Upgrade-Insecure-Requests"="1"
  "DNT"="1"
  "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
  "Referer"="http://ttv.run/index.php"
  "Accept-Encoding"="gzip, deflate"
  "Accept-Language"="en-US,en;q=0.9,de;q=0.8,sk;q=0.7,uk;q=0.6,pl;q=0.5,ru;q=0.4"
} `
-ContentType "application/x-www-form-urlencoded" `
-Body "backurl=%2Findex.php&email=*****&password=*****&remember=on&confirm=on&enter=%D0%92%D0%BE%D0%B9%D1%82%D0%B8"

这是我尝试过的urllib请求

headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-language": "en-US,en;q=0.9,de;q=0.8,sk;q=0.7,uk;q=0.6,pl;q=0.5,ru;q=0.4",
    "cache-control": "max-age=0",
    "content-type": "application/x-www-form-urlencoded",
    "upgrade-insecure-requests": "1",
    "User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"
}

data_w = {
    "referrer": "http://ttv.run/index.php",
    "Content-Type": "application/x-www-form-urlencoded",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": "backurl=%2Findex.php&email=****et&password=*****&remember=on&confirm=on&enter=%D0%92%D0%BE%D0%B9%D1%82%D0%B8",
    "method": "POST",
    "mode": "cors",
    "credentials": "include"
}

data = urllib.parse.urlencode(data_w)
data = data.encode('utf-8')  # data should be bytes
# req = urllib.request.Request("http://ttv.run/auth.php", data)

req = urllib.request.Request("http://ttv.run/auth.php", data=data, headers=headers)
with urllib.request.urlopen(req) as response:
    the_page = response.read()
    # the_page = gzip.decompress(the_page)
    page = the_page.decode('utf-8')

我也试过这个

headers_w = {
    "cache-control": "max-age=0",
    "Origin": "http://ttv.run",
    "Upgrade-Insecure-Requests": "1",
    "DNT": "1",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Referer": "http://ttv.run/index.php",
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "en-US,en;q=0.9,de;q=0.8,sk;q=0.7,uk;q=0.6,pl;q=0.5,ru;q=0.4",
    "body": "backurl=%2Findex.php&email=*****&password=*****&remember=on&confirm=on&enter=%D0%92%D0%BE%D0%B9%D1%82%D0%B8",
    "referrer": "http://ttv.run/auth.php?backurl=%2Fchannels",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",
    "Content-type": "application/x-www-form-urlencoded",
    "method": "POST",
    "mode": "cors",
    "credentials": "include"
}
req = urllib.request.Request("http://ttv.run/auth.php", headers=headers_w)

并带有请求库

response = requests.get('http://ttv.run/auth.php', params=data_w, headers=headers)
response.encoding = response.apparent_encoding
print(response.text[0:400])

还有请求

response = requests.post('http://ttv.run/auth.php', data=data_w, headers=headers)
response.encoding = response.apparent_encoding
print(response.text[0:400])

Tags: runimageauthhttpdataapplicationonresponse