python-3.x - 如何使用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”属性的值。
解决方案
在获取页面源之前添加延迟,因为 QRCode 是在页面加载后异步生成的。
或者您可以定期检查QRCode部分是否存在,停止检查并在出现时获取它。
推荐阅读
- c# - 在类声明与构造函数中初始化对象的区别
- javascript - 字段长度始终 = 1
- google-app-maker - Google App Maker Widget Validate on Datasource select
- xml - 如何使用 xslt 将给定的 xml 转换为如下内容
- python - Python从输入文件中读取行并写回文件
- c# - 有没有办法正确传递包含“?”的字符串 到 .net 核心 web api?
- python - python程序重载'=='运算符以打印具有相同分数的学生的详细信息
- css - 在测试用例上找不到 iframe
- c# - 如何从下拉列表中将项目添加到列表框中
- sql-server - SQL Server 复制:如何找出哪些订阅者附加到分发服务器