python url请求响应解码

时间:2019-01-19 13:11:17

标签: python python-3.x url urllib

我正在尝试从urllib.request响应中获取图像的链接。

我正在尝试从以下页面获取内容:https://drscdn.500px.org/photo/27428737/m%3D900/v2?webp=true&sig=3d3700c82ea515ecc0b66ca265d6909d67861fbe055c0e817b535f75b21c7ebf并对其进行解码,但是encode(“ utf-8”)方法给我错误:'utf-8'编解码器无法解码其中的字节0xff位置0:无效的起始字节。我已经在浏览器控制台中使用document.characterSet检查了页面编码,并且它与utf-8编码匹配。

def ex4():
    url = sys.argv[1]
    r = re.compile(b"<img .*? src=\"([^\"])*\" (.*?)*>")
    try:
        resource = urllib.request.urlopen(url)
        response = resource.read().decode("utf-8")
        print(response)
        obj = r.search(response)
        if obj:
            print(obj.group(1))
        else:
            print("not found")
    except Exception as e:
        print("error: ", e)


ex4()

2 个答案:

答案 0 :(得分:1)

为您提供了二进制图像,因此您可以直接保存或处理该图像。
例如:

url = 'https://drscdn.500px.org/photo/27428737/m%3D900/v2?webp=true&sig=3d3700c82ea515ecc0b66ca265d6909d67861fbe055c0e817b535f75b21c7ebf'
resource = urllib.request.urlopen(url)
response = resource.read()

with open('/tmp/foo.jpg', 'wb') as f:
    f.write(response)

答案 1 :(得分:0)

您试图实现什么?获取图像并保存为文件?如果是,则将其保存在文件中

def ex4():
    url = sys.argv[1]
    try:
        resource = urllib.request.urlopen(url)
        response = resource.read()
        with open('img.png', 'wb') as f:
            f.write(a)
    except Exception as e:
        print("error: ", e)

ex4()