如果URL以"blob:"开头,如何使用Python 3/Selenium下载图像? [英] How to download an image with Python 3/Selenium if the URL begins with "blob:"?
问题描述
使用web.whatsapp.de时,可以看到指向已接收图像的链接可能看起来像这样:
When using web.whatsapp.de one can see that the link to a recieved image may look like this:
blob:https://web.whatsapp.com/3565e574-b363-4aca-85cd-2d84aa715c39
如果将链接复制到地址窗口中,它将打开图像,但是-如果遗漏了"blob",它将仅打开一个新的Web whatsapp窗口.
If the link is copied in to an address window it will open up the image, however - if "blob" is left out - it will simply open a new web whatsapp window.
我正在尝试下载此链接显示的图像.
I am trying to download the image displayed by this link.
但是,使用诸如request,urllib.request甚至BeautifulSoup之类的常用技术总是会遇到困难:URL开头的斑点"会引发错误.
But using common techniques such as using request, or urllib.request or even BeautifulSoup always struggle at one point: The "blob" at the beginning of the url will throw an error.
这些答案使用Python从Blob URL下载文件将错误
URLError: <urlopen error unknown url type: blob>
或错误
InvalidSchema: No connection adapters were found for 'blob:https://web.whatsapp.com/f50eac63-6a7f-48a4-a2b8-8558a9ffe015'
(使用BeatufilSoup)
(using BeatufilSoup)
使用本机方法,例如:
import requests
url = 'https://web.whatsapp.com/f50eac63-6a7f-48a4-a2b8-8558a9ffe015'
fileName = 'test.png'
req = requests.get(url)
file = open(fileName, 'wb')
for chunk in req.iter_content(100000):
file.write(chunk)
file.close()
只会导致与使用BeautifulSoup相同的错误.
Will simply result in the same error as using BeautifulSoup.
我正在使用Python中的Selenium控制Chrome,但是我无法使用提供的链接正确下载图像.
I am controlling Chrome using Selenium in Python, however I was unable to download the image correctly using the provided link.
推荐答案
blob是浏览器存储的原始数据的文件状对象.
A blob is a filelike object of raw data stored by the browser.
您可以在 chrome://blob-internals/
可以通过脚本注入使用Selenium来获取blob的内容.但是,您必须通过在创建Blob的页面/域上运行脚本来遵守跨源策略:
It's possible to get the content of a blob with Selenium with a script injection. However, you'll have to comply to the cross origin policy by running the script on the page/domain that created the blob:
def get_file_content_chrome(driver, uri):
result = driver.execute_async_script("""
var uri = arguments[0];
var callback = arguments[1];
var toBase64 = function(buffer){for(var r,n=new Uint8Array(buffer),t=n.length,a=new Uint8Array(4*Math.ceil(t/3)),i=new Uint8Array(64),o=0,c=0;64>c;++c)i[c]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charCodeAt(c);for(c=0;t-t%3>c;c+=3,o+=4)r=n[c]<<16|n[c+1]<<8|n[c+2],a[o]=i[r>>18],a[o+1]=i[r>>12&63],a[o+2]=i[r>>6&63],a[o+3]=i[63&r];return t%3===1?(r=n[t-1],a[o]=i[r>>2],a[o+1]=i[r<<4&63],a[o+2]=61,a[o+3]=61):t%3===2&&(r=(n[t-2]<<8)+n[t-1],a[o]=i[r>>10],a[o+1]=i[r>>4&63],a[o+2]=i[r<<2&63],a[o+3]=61),new TextDecoder("ascii").decode(a)};
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.onload = function(){ callback(toBase64(xhr.response)) };
xhr.onerror = function(){ callback(xhr.status) };
xhr.open('GET', uri);
xhr.send();
""", uri)
if type(result) == int :
raise Exception("Request failed with status %s" % result)
return base64.b64decode(result)
bytes = get_file_content_chrome(driver, "blob:https://developer.mozilla.org/7f9557f4-d8c8-4353-9752-5a49e85058f5")
这篇关于如果URL以"blob:"开头,如何使用Python 3/Selenium下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!