skip to Main Content

I want to get the view count of a post in telegram. when I get the page source javaScript functions doesn’t executed yet and so the data is not enabled. any help?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait    

driver = webdriver.Firefox()

driver.get("https://t.me/varzesh3/106369")

wait = WebDriverWait(driver, 30)
element = wait.until(EC.visibility_of_any_elements_located((By.CLASS_NAME,'tgme_widget_message_views')))

I even tried it with Chrome driver but no change.

2

Answers


  1. Chosen as BEST ANSWER

    There is a better Idea here. I can use bs4 with url https://t.me/varzesh3/106369?embed=1.

    it's better because when you send the request by this url you have the iframe now.

    and this is the code:

    def generate_post_views(link):
       # https://t.me/varzesh3/106432
       html = requests.get(link + '?embed=1')
       plain_text = html.text
       soup = BeautifulSoup(plain_text, "html.parser")
       try:
           views = soup.find('span', {'class':'tgme_widget_message_views'}).get_text()
       except AttributeError:
           views = 0
       return views
    

  2. It’s not working because the element you want to check is inside an iframe. You need to first switch to IFrame.

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    from selenium.webdriver.common.by import By
    import selenium.webdriver.support.expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait    
    
    driver = webdriver.Chrome()
    
    driver.get("https://t.me/varzesh3/106369")
    iframe = driver.find_element(By.ID,"telegram-post-varzesh3-106369")
    driver.switch_to.frame(iframe)
    wait = WebDriverWait(driver, 30)
    element = wait.until(EC.visibility_of_any_elements_located((By.CLASS_NAME,'tgme_widget_message_views')))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search