1

I am working on a project that requires web scraping. Hence I was looking at the Requests library in Python. In the quickstart page of the library the following code example is given:

>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.text
'[{"repository":{"open_issues":0,"url":"https://github.com/...

On trying out that code I found that the following error is returned when I try to print r.text:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Response' object has no attribute 'text'

What am I doing wrong?

Chaitanya Nettem
  • 1,209
  • 2
  • 23
  • 45
  • 1
    Which version of requests? Can you print `r` before `r.text`? – Alex L Feb 06 '13 at 07:56
  • You might also want to check `r.status_code` is 200 – Alex L Feb 06 '13 at 08:00
  • 1
    try `r.content` -- it should work with an older version of `requests` or upgrade... – root Feb 06 '13 at 08:00
  • which version of requests are you using. do $pip freeze. edit: jk, someone already posted that – Greg Feb 06 '13 at 08:05
  • Yes I can print r before r.text. The page is fetched properly. The status code is 200 and r.content works. But since the example mentions r.text I was wondering why I cant get that to work. I am running the latest version(1.1.0). – Chaitanya Nettem Feb 06 '13 at 08:06
  • what does `requests.__version__` say, and which python version are you running? – root Feb 06 '13 at 08:09
  • Huh. requests has a different version in python3 and python2.7 I wonder why its like that. I was working in python 2.7 and there requests is at verion 0.8. I just checked on python3 and there it is at 1.1.0 – Chaitanya Nettem Feb 06 '13 at 08:14
  • The error doesnt occur in python3. Could anyone explain why this happened? – Chaitanya Nettem Feb 06 '13 at 08:15
  • 1
    Try `which pip` and see what the default is? If you just do `pip install requests` then it will install it for the default Python version only. You would have to specify `pip-2.7 install --upgrade requests`. Clear? – aychedee Feb 06 '13 at 08:19
  • 1
    What do you mean? -- it's because your python3 has the latest version and the 2.7 doesn't... – root Feb 06 '13 at 08:19
  • 1
    One trick, the default version of Python for pip is whichever you installed last. So you could `easy_install-2.7 pip` again to switch this behaviour. – aychedee Feb 06 '13 at 08:21
  • @root Of course I understand that my python3 has a newer version of requests than my python2.7 I am asking why that happened. Why wasnt the newer version installed on both by default. Is that the expected behaviour or did I do something wrong. – Chaitanya Nettem Feb 06 '13 at 08:28
  • @root Thanks for the answer. And aychedee also explained it well. The default version of pip decides this. I think I had used my system's package manager to install requests (sudo apt-get install python-requests) and thats why this happened. – Chaitanya Nettem Feb 06 '13 at 08:30
  • @guy -- glad it helped, here's also another question about installing packages with multiple python versions http://stackoverflow.com/questions/2812520/pip-dealing-with-multiple-python-versions – root Feb 06 '13 at 08:33

1 Answers1

1

try r.content -- it should work with an older version of requests or upgrade. To upgrade requests on python 2.7 use sudo pip-2.7 install requests --upgrade

root
  • 76,608
  • 25
  • 108
  • 120
  • Okay. I assumed this would work. But I just tried it and apparently pip-2.7 is not installed and my package manager can't locate it. – Chaitanya Nettem Feb 06 '13 at 08:40
  • @guy -- did you check the link i posted in the comments http://stackoverflow.com/questions/2812520/pip-dealing-with-multiple-python-versions -- there are multiple alternatives to do it... – root Feb 06 '13 at 08:45