Python Forum

Full Version: module requests, .text extension
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import requests
res = requests.get('https://automatetheboringstuff.com/files/rj.txt')
print(type(res))
print(res.status_code == requests.codes.ok)
print(len(res.text))
print(res.text[:250])

res = requests.get('http://inventwithpython.com/page_that_does_not_exist')
res.raise_for_status()

res = requests.get('http://inventwithpython.com/page_that_does_not_exist')
try:
    res.raise_for_status()
except Exception as exc:
	print(f'There was a problem {exc}')
In lines 5 and 6 .text is used. Why does it have to be used? Why the content of Response type res can't be printed? And if an extension must be added why not .txt?
The res is an object returned from requests.get().
You use .text to get the "text" content of the object.