[英]What should i do to fix the NoSuchElementException error in my python code?
[英]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)
在这段代码中,我在第8行遇到错误
"TypeError: can't use a string pattern on a bytes-like object" And
"File"C:\Python34\lib\re.py", line 210, in findall"
请帮帮我。
urllib.request.urlopen().read()
返回字节字符串。 您将需要对它进行decode()
以获取字符串,例如-
value=urllib.request.urlopen(url).read().decode('cp949')
使用了cp949,因为您似乎在标题中使用了它- # -*- coding: cp949 -*-
,可以使用所需的任何编码,也可以将其保留为空白,因此将使用默认编码对其进行解码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.