56

Note: This is Python 3, there is no urllib2. Also, I've tried using json.loads(), and I get this error:

TypeError: can't use a string pattern on a bytes-like object

I get this error if I use json.loads() and remove the .read() from response:

TypeError: expected string or buffer

>

import urllib.request
import json

response = urllib.request.urlopen('http://www.reddit.com/r/all/top/.json').read()
jsonResponse = json.load(response)

for child in jsonResponse['data']['children']:
    print (child['data']['title'])

Does not work... I have no idea why.

Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
Parseltongue
  • 11,157
  • 30
  • 95
  • 160

4 Answers4

105

Try this:

jsonResponse = json.loads(response.decode('utf-8'))
MRAB
  • 20,356
  • 6
  • 40
  • 33
43

Use json.loads not json.load.

(load loads from a file-like object, loads from a string. So you could just as well omit the .read() call instead.)

Katriel
  • 120,462
  • 19
  • 136
  • 170
  • 1
    Does not work. If you include the .read, this error is prompted: TypeError: can't use a string pattern on a bytes-like object If you remove the .read(), you get this error: TypeError: expected string or buffer – Parseltongue Jun 30 '11 at 22:33
2

I'm not familiar with python 3 yet, but it seems like urllib.request.urlopen().read() returns a byte object rather than string.

You might try to feed it into a StringIO object, or even do a str(response).

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
0

I got the same error {AttributeError: 'bytes' object has no attribute 'read'} in python3. This worked for me later without using json:

from urllib.request import urlopen
from bs4 import BeautifulSoup

url = 'https://someurl/'
page = urlopen(url)
html = page.read()
soup = BeautifulSoup(html)
print(soup.prettify('latin-1'))
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
Banjali
  • 181
  • 1
  • 5
  • 2
    Welcome to SO. This question already has a highly up-voted accepted answer. Suggesting an alternative that, whilst it may work, does not actually address the issue asked in the OP is not that useful. See https://stackoverflow.com/help/how-to-answer – Nick Jul 17 '18 at 07:56