2014-09-29 80 views
0

我對python請求模塊有錯誤。這是我的代碼。Python請求錯誤版本2.4.1

#!/usr/bin/env python3 
import requests 

url = 'http://www.mingeford365.co.uk/forum/ucp.php?mode=login' 

logininfo = {'username': '', 
      'password': ''} 

headers = {'Host': 'www.mingeford365.co.uk', 
      'User-Agent' : 'Mozilla/5.0 (x11; Ubuntu; Linux x86; rv:28.0) Gecko/20100101 Firefox/28.0', 
      'Accept': 'text/html, application/xhtml+xhtml,application/xml;q=0.9,*/*;q=0.8', 
      'Accept-Language': 'en-gb,en;q=0.5', 
      'Accept-Encoding': 'gzip, deflate',         
      'referer': 'http://www.mingeford365.co.uk/forum/viewforum.php?f=4', 
      'Cookie' : '', 
      'Connection' : 'keep-alive', 
      'Content-Type': 'application/x-www-form-urlencoded'} 

r = requests.session() 
r.post(url,params=logininfo,headers=headers) 

print (r.text) 

我不斷收到錯誤。

Traceback (most recent call last): 
    File "./BasicLogin.py", line 22, in <module> 
    print (r.text) 
AttributeError: 'Session' object has no attribute 'text' 

我在這裏發現了一個類似的問題。 AttributeError: 'Response' object has no attribute 'text'

但是,解決方案是安裝最新版本的請求。我已經安裝了最新版本,我重新安裝了pip和pip3,並且兩者都重新下載了請求,然後嘗試升級,並告訴我我有最新版本。

但是我仍然得到相同的錯誤信息。我已經嘗試了python 3和python 2.7中的代碼,但我仍然得到相同的錯誤,我試過r.status_coder.textr.content,但仍然得到會話沒有屬性錯誤。

我不知道還能做些什麼。

回答

0

r仍然是會議。您需要使用的r.post()而不是返回值:更名爲目的

session = requests.session() 
response = session.post(url, params=logininfo, headers=headers) 
print(response.text) 

變量。請不要成爲那些用難看的變量名稱感染代碼的人。 :)

0

r是你的會話對象,而不是響應。該r.post()方法返回的響應,使用代替:

response = r.post(url, params=logininfo, headers=headers) 
print(response.text) 

你可能想避免1個字母變量;使用session您的會話對象,例如:

session = requests.session() 
response = session.post(url, params=logininfo, headers=headers) 
print(response.text) 

現在表現得更加明顯,你使用的是什麼對象。