【发布时间】:2015-07-28 20:37:58
【问题描述】:
我一直在尝试编写一个 python 脚本来让我从 whatsapp 网络上获取二维码。我使用 selenium,但似乎无法编写。
QR 图像是由 javascript live(因此是 selenium)生成的 PNG,其中它被转换为 Base64 中的 PNG 并由浏览器转换为图像。
到目前为止我的代码是这样做的:
import base64
from selenium import webdriver
url = "https://web.whatsapp.com"
init_search = "<img style=\"display: block;\" src=\"data:image/png;base64,"
term_search = "\"></div>"
browser = webdriver.Firefox()
browser.get(url)
html_source = browser.page_source
start = html_source.find(init_search)
end = html_source[start:].find(term_search)
raw_png = html_source[start+len(init_search):end]
print html_source[start:end]
print base64.b64decode(raw_png)
browser.close()
但它并不是很稳定。如何做到这一点,如果可能,无需实际打开浏览器?
【问题讨论】:
标签: javascript python selenium base64 png