【问题标题】:Error 405 when trying to Post a Request with Python尝试使用 Python 发布请求时出现错误 405
【发布时间】: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


    【解决方案1】:
    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)
    

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      相关资源
      最近更新 更多