How do I find the images from the GIF section on Tenor via Selenium + Python?

Question:

I want to find GIFs on Tenor, and download them.

Example: URL : https://tenor.com/search/dance-gifs
I want to download the GIFs section which seem to have this HTML code:

<div class="Gif "><img src="https://media.tenor.com/Lbrr3HR3CnkAAAAM/snoop-dogg-rap.gif" width="290" height="460.0454545454545" alt="Snoop Dogg Rap GIF - Snoop Dogg Rap Hip Hop GIFs"></div>

I got some code from GitHub that already does so by finding all elements with the tag ‘img’:

def scrape_page():
    driver = webdriver.Chrome(PATH)
    url = 'https://tenor.com/search/dance-gifs'
    print(f"Scrape: {url}")
    driver.get(url)
    time.sleep(1)

    try:
        print("Page is ready!")

        elements = driver.find_elements(By.TAG_NAME, 'img')[10:20]
        container_links = []
        for element in elements:
            container_links.append(element.get_attribute("src"))

However, this code also downloads the STICKERS section of the site (https://tenor.com/search/dance-gifs), whereas I want to only download the GIFs section.

I have tried multiple things without success.

Asked By: ploomplam

||

Answers:

To identify the <img> only from the GIFs section you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://tenor.com/en-GB/search/dance-gifs')
    elements = driver.find_elements(By.CSS_SELECTOR, 'div.Gif > img')
    
  • Using XPATH:

    driver.get('https://tenor.com/en-GB/search/dance-gifs')
    elements = driver.find_elements(By.XPATH, '//div[@class='Gif ']/img')
    
Answered By: undetected Selenium