首页 > 解决方案 > 如何使用python从whatsapp web中提取二维码?

问题描述

我曾尝试使用 python 和 selenium 提取二维码并且能够提取但有时二维码未加载。

from selenium import webdriver  
from selenium.common.exceptions import NoSuchElementException  
from selenium.webdriver.common.keys import Keys  
from bs4 import BeautifulSoup

browser = webdriver.Firefox()  
browser.get('https://web.whatsapp.com/')  
html_source = browser.page_source  
browser.quit()

soup = BeautifulSoup(html_source, 'html.parser')
divs = soup.find('div', {"class":"_1pw2F"})
print(divs.attrs["data-ref"])
# in whatsapp web, qr code is the value of "data-ref" attribute of div element with class "_1pw2F"

有时没有加载 div 元素的“data-ref”属性的值。

标签: python-3.xseleniumweb-scrapingdata-extraction

解决方案


在获取页面源之前添加延迟,因为 QRCode 是在页面加载后异步生成的。

或者您可以定期检查QRCode部分是否存在,停止检查并在出现时获取它。


推荐阅读