5

I'm trying to read a website's content but I get an empty bytes object, b''.

import urllib3
from urllib3 import PoolManager
urllib3.disable_warnings()
https = PoolManager()

r = https.request('GET', 'https://minemen.club/leaderboards/practice/')

print(r.status)
print(r.read())

When I open the URL in a web browser I see the website, and r.status is 200 (success).

Why does r.read() not return the content?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
sef sf
  • 117
  • 1
  • 8
  • 2
    I'd skip this line `urllib3.disable_warnings()` and see if you get any useful message. – norok2 May 04 '20 at 13:02
  • There is no warning in this case, but you're right to say that the warnings should not be disabled, especially when things don't go as expected. – Keldorn May 04 '20 at 13:07

2 Answers2

8

What makes you think it is wrong? Try the following, you'll have much more output:

print(r.data)

Check HTTPResponse to see how to use the r object you got.

Keldorn
  • 1,980
  • 15
  • 25
1

This is how urllib3.response.HTTPResponse.read is supposed to work.

It is explained for example here by one of the contributors to urllib3:

This is about documentation. You cannot use read() by default, because by default all the content is consumed into data. If you want read() to work, you need to set preload_content=True on the call to urlopen. Want to give that a try?

So you can simply use r.data.

djvg
  • 11,722
  • 5
  • 72
  • 103
mkrieger1
  • 19,194
  • 5
  • 54
  • 65