Skip to content
Advertisement

Python web-scraping error – TypeError: can’t use a string pattern on a bytes-like object

I want to build a web scraper. Currently, I’m learning Python. This is the very basics!

Python Code

import urllib.request
import re

htmlfile = urllib.request.urlopen("http://basketball.realgm.com/")

htmltext = htmlfile.read()
title = re.findall('<title>(.*)</title>', htmltext)

print (htmltext)

Error:

  File "C:Python33libre.py", line 201, in findall
    return _compile(pattern, flags).findall(string)
TypeError: can't use a string pattern on a bytes-like object

Advertisement

Answer

You have to decode your data. Since the website in question says

charset=iso-8859-1

use that. utf-8 won’t work in this case.

htmltext = htmlfile.read().decode('iso-8859-1')
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement