【问题标题】:I have this output from scraping an image from a website我从网站上抓取图像得到了这个输出
【发布时间】:2021-07-25 02:58:10
【问题描述】:

当我从 url 请求图像时,我正在使用此代码。

从 urllib.request 导入 urlretrieve

导入请求

image_url = "https://www.coinspot.com.au/public/img/coinmd/steem.png?v=180"

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko)

Chrome/39.0.2171.95 Safari/537.36'}

response = requests.get(image_url, headers=headers)

打印(response.content)

我得到的东西看起来像这样(小样本)-

b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x80\x00\x00\x00\x80
\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x c9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f \x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x c9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f\xc9\x17\x1f ca\x02\t\xc4\x05\r\xc5\n\x13\xc6\x03\x0b\xc4\x08\x10\xc6\x0e\x17\xc7\x dc\xfd\xfd\xff\x85\x89\xe3Y_\xd9JP\xd5(0\xce\xf7\xf8\xfe\xec\xed\xfbSX

谁知道这个可以转换成png文件吗?我不知道我在看什么。当我将输出保存到 png 文件时,它不显示,但收到一条消息,它太大或损坏。

【问题讨论】:

    标签: python image screen-scraping


    【解决方案1】:

    图片的十六进制

    您可以像这样简单地将其写入具有 .png 扩展名的文件

    data = bytes.fromhex(str(response.content)[2:])
    
    with open('image.png', 'wb') as file:
        file.write(data)
    
    

    【讨论】:

    • 我刚刚尝试了上面的 - 文件“C:\Crypto\imageget.py”,第 14 行,在 data = bytes.fromhex(response.content[2:]) TypeError: fromhex () 参数必须是 str,而不是字节所以我将其更改为 - Traceback(最近一次调用最后一次):文件“C:\Crypto\imageget.py”,第 14 行,在 data = str.fromhex(response. content[2:]) AttributeError: type object 'str' has no attribute 'fromhex'
    • 评论时如何转到下一行?
    • 我已经对我的答案进行了编辑..你现在可以检查一下
    • 好的!试过了,这次我得到了错误 - ValueError: non-hexadecimal number found in fromhex() arg at position 0 位置 1 的输入代码看起来像这样 - b'\x89PNG\r\n\x1a\n\x0 似乎抱怨 b' 或至少 b
    猜你喜欢
    • 2021-01-11
    • 1970-01-01
    • 2020-08-05
    • 2016-09-08
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多