0
# -*- 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.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
강병찬
  • 937
  • 1
  • 8
  • 9

1 Answers1

1

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.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176