I'm having an error with python requests module. This is my code.
#!/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)
I keep getting the error.
Traceback (most recent call last):
File "./BasicLogin.py", line 22, in <module>
print (r.text)
AttributeError: 'Session' object has no attribute 'text'
I found a similar question on here before. AttributeError: 'Response' object has no attribute 'text'
However, the solution was to have the most recent version of requests installed. I already have the most recent version installed, ive reinstalled pip and pip3 and redownloaded requests for both, have then tried the upgrade and it tells me i have the up to date version.
However I still get the same error message. I've tried the code in python 3 and python 2.7 and I still get the same error, I've tried r.status_code
, r.text
and r.content
and still get the sessions has no attribute error.
I don't know what else to do.
r
is still the session. You need to use the return value of r.post()
instead:
session = requests.session()
response = session.post(url, params=logininfo, headers=headers)
print(response.text)
Variables renamed on purpose. Please don't be the guy who infects code with ugly variable names. :)
r
is your session object , not the response. The r.post()
method returns a response, use that instead:
response = r.post(url, params=logininfo, headers=headers)
print(response.text)
You probably want to avoid 1 letter variables; use session
for your session object for example:
session = requests.session()
response = session.post(url, params=logininfo, headers=headers)
print(response.text)
Now it is much clearer what object you are using.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.