Python:从 url 中检索文件并生成数据:URI
Python: Retrieve a file from url and generate data: URI
我有一张 url 的头像
url = "http://www.gravatar.com/avatar/0eee0fa5a325a984dab6c1e9c7a1968f?d=retro&s=40"
我想为它生成 data:URI 就像在 python
中一样
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAIAAAADnC86AAAAjklEQVRYhWP0E3dnIAUY9jdiFT9fWE+SOUwkqaYiGHkWs+CSQI5LUuOPGL0jL6gHzGJGYvIxrryLCxCTJkZeUI9aPGoxzQDOshoXwJVHSc3rIy+oB0dZTa32FDHmjLygHnz5GDk+KKmPcekdeUE9OPIxMsDVNiYmj462qweVxUS1q2kBRl5Qj1o8ajHNAABQ3ieujBTqugAAAABJRU5ErkJggg==
import base64
import requests
url = "https://www.gravatar.com/avatar/0eee0fa5a325a984dab6c1e9c7a1968f?d=retro&s=40"
response = requests.get(url)
content_type = response.headers["content-type"]
encoded_body = base64.b64encode(response.content)
print("data:{};base64,{}".format(content_type, encoded_body.decode()))
回复:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAIAAAADnC86AAAAjklEQVRYhWP0E3dnIAUY9jdiFT9fWE+SOUwkqaYiGHkWs+CSQI5LUuOPGL0jL6gHzGJGYvIxrryLCxCTJkZeUI9aPGoxzQDOshoXwJVHSc3rIy+oB0dZTa32FDHmjLygHnz5GDk+KKmPcekdeUE9OPIxMsDVNiYmj462qweVxUS1q2kBRl5Qj1o8ajHNAABQ3ieujBTqugAAAABJRU5ErkJggg==
我有一张 url 的头像
url = "http://www.gravatar.com/avatar/0eee0fa5a325a984dab6c1e9c7a1968f?d=retro&s=40"
我想为它生成 data:URI 就像在 python
中一样data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAIAAAADnC86AAAAjklEQVRYhWP0E3dnIAUY9jdiFT9fWE+SOUwkqaYiGHkWs+CSQI5LUuOPGL0jL6gHzGJGYvIxrryLCxCTJkZeUI9aPGoxzQDOshoXwJVHSc3rIy+oB0dZTa32FDHmjLygHnz5GDk+KKmPcekdeUE9OPIxMsDVNiYmj462qweVxUS1q2kBRl5Qj1o8ajHNAABQ3ieujBTqugAAAABJRU5ErkJggg==
import base64
import requests
url = "https://www.gravatar.com/avatar/0eee0fa5a325a984dab6c1e9c7a1968f?d=retro&s=40"
response = requests.get(url)
content_type = response.headers["content-type"]
encoded_body = base64.b64encode(response.content)
print("data:{};base64,{}".format(content_type, encoded_body.decode()))
回复:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAIAAAADnC86AAAAjklEQVRYhWP0E3dnIAUY9jdiFT9fWE+SOUwkqaYiGHkWs+CSQI5LUuOPGL0jL6gHzGJGYvIxrryLCxCTJkZeUI9aPGoxzQDOshoXwJVHSc3rIy+oB0dZTa32FDHmjLygHnz5GDk+KKmPcekdeUE9OPIxMsDVNiYmj462qweVxUS1q2kBRl5Qj1o8ajHNAABQ3ieujBTqugAAAABJRU5ErkJggg==