Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract qr code from whatsapp web with python?

I have tried extracting qr code with python and selenium and able to extract but sometimes qr code is not loading.

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"

sometimes the value of "data-ref" attribute of div element is not loading.

like image 640
Jay Singh Avatar asked Apr 20 '26 05:04

Jay Singh


1 Answers

Add a delay before you get the page source, as the QRCode is generated async after page load.

Or you can periodically check if the QRCode section exist, stop the checking and get it when it appears.

like image 100
MiDaa Avatar answered Apr 23 '26 01:04

MiDaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!