[英]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.