如何处理来自 urllib.request.urlopen() 的响应编码,以避免 TypeError: can't use a string pattern on a bytes-like object

发布于 2024-10-17 12:48:01 字数 324 浏览 1 评论 0原文

我尝试使用 urllib.request.urlopen() 打开网页,然后使用正则表达式搜索它,但这会出现以下错误:

类型错误:无法在类似字节的对象上使用字符串模式

我明白为什么,urllib.request.urlopen()返回一个字节流,所以re不知道要使用的编码。在这种情况下我该怎么办?有没有办法在 urlrequest 中指定编码方法,或者我需要自己重新编码字符串?如果是这样,我想做什么,我假设我应该从标头信息或编码类型(如果在 html 中指定)读取编码,然后将其重新编码为该编码?

I'm trying to open a webpage using urllib.request.urlopen() then search it with regular expressions, but that gives the following error:

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

I understand why, urllib.request.urlopen() returns a bytestream, so re doesn't know the encoding to use. What am I supposed to do in this situation? Is there a way to specify the encoding method in a urlrequest maybe or will I need to re-encode the string myself? If so what am I looking to do, I assume I should read the encoding from the header info or the encoding type if specified in the html and then re-encode it to that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

水晶透心 2024-10-24 12:48:01

对于我来说,解决方案如下(python3):

resource = urllib.request.urlopen(an_url)
content =  resource.read().decode(resource.headers.get_content_charset())

As for me, the solution is as following (python3):

resource = urllib.request.urlopen(an_url)
content =  resource.read().decode(resource.headers.get_content_charset())
一笔一画续写前缘 2024-10-24 12:48:01

您只需要使用 Content-Type 标头(通常是最后一个值)来解码响应。 教程中也给出了一个示例。

output = response.decode('utf-8')

You just need to decode the response, using the Content-Type header typically the last value. There is an example given in the tutorial too.

output = response.decode('utf-8')
平生欢 2024-10-24 12:48:01

最近两天我也遇到了同样的问题。我终于有解决办法了。
我正在使用 urlopen() 返回的对象的 info() 方法:

req=urllib.request.urlopen(URL)
charset=req.info().get_content_charset()
content=req.read().decode(charset)

I had the same issues for the last two days. I finally have a solution.
I'm using the info() method of the object returned by urlopen():

req=urllib.request.urlopen(URL)
charset=req.info().get_content_charset()
content=req.read().decode(charset)
口干舌燥 2024-10-24 12:48:01

这是一个简单的 http 请求示例(我测试过并且有效)...

address = "http://stackoverflow.com"    
urllib.request.urlopen(address).read().decode('utf-8')

请务必阅读文档。

https://docs.python.org/3/library/urllib。 request.html

如果你想做一些更详细的GET/POST REQUEST。

import urllib.request
# HTTP REQUEST of some address
def REQUEST(address):
    req = urllib.request.Request(address)
    req.add_header('User-Agent', 'NAME (Linux/MacOS; FROM, USA)')
    response = urllib.request.urlopen(req)
    html = response.read().decode('utf-8')  # make sure its all text not binary
    print("REQUEST (ONLINE): " + address)
    return html

Here is an example simple http request (that I tested and works)...

address = "http://stackoverflow.com"    
urllib.request.urlopen(address).read().decode('utf-8')

Make sure to read the documentation.

https://docs.python.org/3/library/urllib.request.html

If you want to do something more detailed GET/POST REQUEST.

import urllib.request
# HTTP REQUEST of some address
def REQUEST(address):
    req = urllib.request.Request(address)
    req.add_header('User-Agent', 'NAME (Linux/MacOS; FROM, USA)')
    response = urllib.request.urlopen(req)
    html = response.read().decode('utf-8')  # make sure its all text not binary
    print("REQUEST (ONLINE): " + address)
    return html
自我难过 2024-10-24 12:48:01

对于请求

import requests

response = requests.get(URL).text

With requests:

import requests

response = requests.get(URL).text
你如我软肋 2024-10-24 12:48:01
urllib.urlopen(url).headers.getheader('Content-Type')

将输出如下内容:

text/html;字符集=utf-8

urllib.urlopen(url).headers.getheader('Content-Type')

Will output something like this:

text/html; charset=utf-8

奢望 2024-10-24 12:48:01

发出请求后 req = urllib.request.urlopen(...) 您必须通过调用 html_string = req.read() 来读取请求,这将为您提供然后您可以按照您想要的方式解析字符串响应。

after you make a request req = urllib.request.urlopen(...) you have to read the request by calling html_string = req.read() that will give you the string response that you can then parse the way you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文