尝试使用Python发布请求时出现错误405

时间:2019-04-15 12:38:54

标签: python beautifulsoup request http-post

当我尝试创建发布请求时,我只会收到错误405或转发到主页。我正在尝试发送标头和身份验证。为了能够连接到URL,我需要更改什么?

我已经尝试在连接时交换data = auth和headers = headers的顺序,但是它什么也没做,而且我尝试了另一个不使用csrf令牌的网站,但也失败了。


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告诉我标题不正确,或者我被转发到了首页。

1 个答案:

答案 0 :(得分:0)

  1. 您不需要在标题中传递cookie,因为会话可以这样做
  2. 您的标头字典错误(请检查引号和逗号)
  3. 您不需要“身份验证”,只需执行一个发帖请求

这是工作示例:

import requests
from bs4 import BeautifulSoup

headers = {
    '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"
}

# 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/', headers=headers)

# extract the token
soup = BeautifulSoup(response.text)
for n in soup('input'):
    if n['name'] == 'testdummy':
        token = n['value']
        break


data = {
    'value': 'https://soundcloud.com/bxxmbastic/fygb-flip',
    'testdummy': token
}

# send post data
response = s.post('https://www.klickaud.com/download.php', headers=headers, data=data)
print(response.text)