You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Selenium+Python获取Tenor网站GIF专区图片并下载?

解决Tenor仅爬取GIF专区内容的方案

原代码通过By.TAG_NAME定位所有img元素,范围过宽导致混入贴纸内容。根据你提供的HTML结构,GIF专区的图片都嵌套在class为Gif的div容器内,我们可以通过CSS选择器精准定位来解决这个问题。

修改后的核心代码如下:

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!")

        # 仅定位class为Gif的div下的img元素,排除贴纸内容
        elements = driver.find_elements(By.CSS_SELECTOR, 'div.Gif img')
        # 可选:如果你需要取特定范围的GIF,比如第10到20个,就用切片
        # elements = driver.find_elements(By.CSS_SELECTOR, 'div.Gif img')[10:20]
        container_links = []
        for element in elements:
            src = element.get_attribute("src")
            # 可选:过滤掉非GIF格式的链接(Tenor的GIF链接通常包含tenor.com且后缀为.gif)
            if src and 'tenor.com' in src and src.endswith('.gif'):
                container_links.append(src)

关键说明

  • 使用div.Gif img这个CSS选择器,直接锁定GIF专区内的图片元素,完全避开贴纸专区的内容。
  • 额外添加的链接过滤逻辑可以进一步确保只收集有效的GIF资源,避免意外混入其他类型的图片链接。

内容的提问来源于stack exchange,提问作者ploomplam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 15:31:49