【发布时间】:2019-09-05 10:46:24
【问题描述】:
当我尝试创建发布请求时,我只是收到错误 405 或被转发到主页。我正在尝试发送标头和身份验证。为了能够连接到 URL,我需要进行哪些更改?
我已经尝试在连接时交换 data=auth 和 headers=headers 的顺序但它没有做任何事情,我还尝试了另一个不使用 csrf-tokens 的网站,但它也失败了。
import requests
from bs4 import BeautifulSoup
# need to capture a valid csrf token
# first visit the login page to generate one
s = requests.session()
response = s.get('https://www.klickaud.com/')
# extract the token
soup = BeautifulSoup(response.text)
for n in soup('input'):
if n['name'] == 'testdummy':
token = n['value']
break
tokencsrf ='testdummy =' + token
# now post to that login page with some valid credentials and the token
auth = {
'value': 'https://soundcloud.com/bxxmbastic/fygb-flip'
,'testdummy': token
}
headers = {
'cookie': '__cfduid=d6cd11b0c476cdcd9364e010aebc3e1b01555296698; PHPSESSID=2eh4q7fndr2srru232bbeqc036'
'origin: https://www.klickaud.com'
,'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
}
s.post('https://www.klickaud.com/download.php',headers=headers,data=auth)
#now we should be authenticated, try visiting a protected page
response = s.post('https://www.klickaud.com/download.php', headers=headers, data=auth)
print(response.text)
我希望能够使用 beautifulsoup 解析网站,但是当我请求它时,我要么得到错误 405,告诉我标题不正确,要么我被转发到主页。
【问题讨论】:
标签: python beautifulsoup request http-post