gpt4 book ai didi

正则表达式上的 Python TypeError

转载 作者:IT老高 更新时间:2023-10-28 20:29:51 26 4
gpt4 key购买 nike

所以,我有这个代码:

url = 'http://google.com'
linkregex = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?>')
m = urllib.request.urlopen(url)
msg = m.read()
links = linkregex.findall(msg)

然后python返回这个错误:

links = linkregex.findall(msg)
TypeError: can't use a string pattern on a bytes-like object

我做错了什么?

最佳答案

TypeError: can't use a string patternon a bytes-like object

what did i do wrong??

您在字节对象上使用了字符串模式。改用字节模式:

linkregex = re.compile(b'<a\s*href=[\'|"](.*?)[\'"].*?>')
^
Add the b there, it makes it into a bytes object

(ps:

 >>> from disclaimer include dont_use_regexp_on_html
"Use BeautifulSoup or lxml instead."

)

关于正则表达式上的 Python TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5184483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com