【问题标题】:What should I do this error in Python在 Python 中我应该怎么做这个错误
【发布时间】:2015-10-18 12:03:15
【问题描述】:
# -*- 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"

请帮帮我。

【问题讨论】:

  • 你能打印type(value)
  • Python TypeError on regex的可能重复
  • @VigneshKalai 它是
  • @Andersson 你是对的
  • @Andersson 谢谢。我该如何解决?

标签: python findall


【解决方案1】:

urllib.request.urlopen().read() 返回字节串。你需要decode()它来获取字符串,示例-

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

使用 cp949 因为你似乎在你的标题中使用它 - # -*- coding: cp949 -*- ,你可以使用任何你想要的编码,你也可以将它留空,所以它使用默认编码解码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多