使用requests_html爬取Gogohd下载链接失败求助
解决gogohd.pro下载链接爬取失败的问题
可能的原因及对应解决方案
1. 未处理动态JS渲染
目标网站的下载链接大概率是通过JavaScript动态生成的,requests_html默认发送的GET请求不会自动渲染JS,所以拿不到动态加载的链接。
修改requests_html代码,添加JS渲染步骤,并设置合法的User-Agent:
from requests_html import HTMLSession URL = 'https://gogohd.pro/download?id=OTk1OTk=&typesub=Gogoanime-SUB&title=Dragon+Ball+Super+Episode+100' session = HTMLSession() # 设置模拟浏览器的User-Agent headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' } r = session.get(URL, headers=headers) # 渲染JS,等待页面加载完成 r.html.render(timeout=20) # 筛选下载链接,通常这类链接包含download或特定域名 for link in r.html.links: if 'download' in link or 'gogohd' in link: print(link)
2. Selenium未正确等待页面加载或处理iframe
如果用Selenium,可能是页面还没加载完就开始提取元素,或者下载链接嵌套在iframe中。
修改Selenium代码,添加显式等待,并检查iframe:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC URL = 'https://gogohd.pro/download?id=OTk1OTk=&typesub=Gogoanime-SUB&title=Dragon+Ball+Super+Episode+100' driver = webdriver.Chrome() driver.get(URL) # 等待页面加载,最多20秒 wait = WebDriverWait(driver, 20) # 检查是否存在iframe,切换到iframe中 try: iframe = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'iframe'))) driver.switch_to.frame(iframe) except: pass # 如果没有iframe,直接继续 # 提取所有链接,筛选下载相关的 links = driver.find_elements(By.TAG_NAME, 'a') for link in links: href = link.get_attribute('href') if href and ('download' in href or 'gogohd' in href): print(href) driver.quit()
3. 反爬机制拦截
如果上述方法还是不行,可能网站检测了请求特征:
- 尝试添加Cookie:先手动在浏览器打开目标页面,复制Cookie,加到请求头中。
- 避免频繁请求:添加随机延时,模拟人类操作节奏。
内容的提问来源于stack exchange,提问作者Syed Abdullah
相关产品推荐
相关产品推荐

