Home > database >  Finding Valid Telegram Channel using selenium python
Finding Valid Telegram Channel using selenium python

Time:01-17

Hello good morning/evening! i want to ask, is there a way to differentiate this two type of way telling that those channels does not exist? I'm creating a tool to find existing channel on telegram. Here are the examples.

Unavalable Telegram Channel 1

Unavailable Telegran Channel 2

When i try to run my code, the channels that does not exist still counts as an existing channel.

Code Output

Here is the code that use, to differentiate this 2 type of way telegram telling that the channel does not exist. I used the help of selenium and chrome driver to find the existing telegram channel.

for ss in cli1:
        driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
        driver.maximize_window()
        driver.implicitly_wait(10)
        driver.get(f"https://t.me/{ss}")
        try:
            if WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div/div/div[1]/div[2]/a/div'))):
                print(f'URL https://t.me/{ss} is not available')
                Not_Exist.append(ss)
            elif WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div/div[2]/a'))):
                print(f'URL https://t.me/{ss} is not available')
                Not_Exist.append(ss)
        except:
            print(f'URL https://t.me/{ss} is valid')
            exists.append(ss)
        driver.quit()
        sleep(5)

Is there a way for a telegram channels that does not exist still say no available, instead of making the site available but it doesn't have any channel at all.Here are the link examples. Non existing site 1 https://t.me/minecraft Existing Site https://t.me/minecraftapkdownload Non Existing site 2https://telegram.org/

Thanks for the help!

CodePudding user response:

In your code, in the try block, make the below changes

    try:
        if WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSSSELECTOR, 'tgme_action_button_new shine'))):
            print(f'URL https://t.me/{ss} is valid')
            exists.append(ss)
            
    except:
        print(f'URL https://t.me/{ss} is not available')
        Not_Exist.append(ss)
        

I would recommend you to use path made up of classnames/ids etc. Paths like/html/body/div[2]/div[2]/div/div/div[1]/div[2]/a/div are prone to throw errors even with minimal changes to the GUI

In the above code, I have assumed that the View in Telegram button is shown if a channel is valid and for all other pages no such button is shown.

The class for the button is 'tgme_action_button_new shine'

The code checks if this button exists, if yes then it adds the page to the valid list, else to the invalid list

  • Related