강병찬
강병찬

Reputation: 977

What should I do this error in Python

# -*- coding: cp949 -*-

import urllib.request
import re
url="http://google.co.kr"
value=urllib.request.urlopen(url).read()
par='<title>(.+?)</title>'
result=re.findall(par,value)
print(result)

In this code I met Error in line 8

"TypeError: can't use a string pattern on a bytes-like object" And
"File"C:\Python34\lib\re.py", line 210, in findall"

Help me please.

Upvotes: 0

Views: 66

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90919

urllib.request.urlopen().read() returns byte-string. You will need to decode() it to get the string, Example -

value=urllib.request.urlopen(url).read().decode('cp949')

Used cp949 since you seem to be using that in your header - # -*- coding: cp949 -*- , you can use any encoding you want, you can also leave it blank, so the it gets decoded using the default encoding.

Upvotes: 1

Related Questions