详情介绍
javascript
list=""
document.querySelectorAll("a").forEach(element => list = list + element.href + '\n')
console.log(list)
此脚本会抓取页面中所有a标签的链接,并通过换行符拼接成列表。若需筛选特定链接(如包含“download”或“.zip”),可修改条件为`element.href.includes('keyword')`。
2. 安装扩展程序简化操作:访问Chrome应用商店,搜索并安装“Link Grabber”或“Web Scraper”。
- Link Grabber:点击插件图标→自动提取所有链接→在弹窗中输入过滤词(如`.pdf`)→点击“Copy”复制结果。
- Web Scraper:拖动鼠标选择含下载链接的区域→自动生成抓取规则→导出为CSV或JSON文件。
3. 编写自动化脚本(进阶):使用Python配合BeautifulSoup库:
- 安装依赖:`pip install requests beautifulsoup4`
- 示例代码:
python
import requests
from bs4 import BeautifulSoup
url = "目标网页地址"
response = requests.get(url)
soup = BeautifulSoup(response.text, '.parser')
links = [a['href'] for a in soup.find_all('a', href=True) if 'download' in a['href']]
print('\n'.join(links))
保存为`.py`文件后运行,可批量提取含“download”的链接。
4. 解决动态加载链接问题:若下载链接通过JavaScript动态生成,需使用Selenium模拟浏览器行为:
- 安装驱动(如ChromeDriver)并运行以下代码:
python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("目标网页地址")
links = [elem.get_attribute('href') for elem in driver.find_elements_by_tag_name('a')]
print('\n'.join(links))
driver.quit()