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

如何用Python Selenium获取noscript内meta标签的url属性?

解决SourceForge页面noscript内meta标签url属性爬取问题

你需要从https://sourceforge.net/projects/mingw/files/latest/download页面提取noscript标签内meta标签的url属性值,但当前代码循环返回None,核心问题在于:

Chrome无头模式默认启用JavaScript,而noscript标签的内容仅在浏览器禁用JS时才会被渲染解析,导致你的驱动无法获取到noscript内部的meta元素;同时遍历所有meta标签的方式没有针对性,效率低下。

解决方案一:禁用JavaScript渲染noscript内容

修改ChromeOptions添加禁用JS的参数,让浏览器解析noscript内的元素,再精准定位目标标签:

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

download_url = 'https://sourceforge.net/projects/mingw/files/latest/download'

op = webdriver.ChromeOptions()
op.add_argument('headless')
op.add_argument('--disable-javascript')  # 禁用JS,触发noscript内容渲染
service_obj = Service('chromedriver.exe')
driver = webdriver.Chrome(service=service_obj, options=op)

try:
    driver.get(download_url)
    # 定位noscript标签内带url属性的meta元素
    noscript_meta = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//noscript/meta[@url]'))
    )
    target_url = noscript_meta.get_attribute('url')
    print(target_url)
finally:
    driver.quit()

解决方案二:直接解析页面源码(更轻量)

无需启动浏览器,用requests获取页面源码后,通过BeautifulSoup解析noscript内的目标元素,效率更高:

import requests
from bs4 import BeautifulSoup

download_url = 'https://sourceforge.net/projects/mingw/files/latest/download'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}

response = requests.get(download_url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')

# 逐层定位目标元素
noscript_tag = soup.find('noscript')
if noscript_tag:
    meta_tag = noscript_tag.find('meta', attrs={'url': True})
    if meta_tag:
        print(meta_tag['url'])

两种方案均可精准提取目标链接,方案二更适合无浏览器交互需求的静态内容爬取场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 16:09:26