本文目录导读:

的方法,根据不同场景选择最适合的方式:
浏览器开发者工具(最常用)
查看页面源代码
// 在控制台执行 document.documentElement.outerHTML // 或查看整个页面源代码 view-source:https://example.com
提取特定元素
// 获取页面标题
document.title
// 获取所有链接
document.querySelectorAll('a')
// 获取特定ID的元素
document.getElementById('content')
// 获取所有图片
document.querySelectorAll('img')
Python库提取
使用requests + BeautifulSoup
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 提取所有文本
text = soup.get_text()
# 提取特定标签
links = soup.find_all('a')
使用Selenium(处理动态页面)
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
# 提取页面内容
page_source = driver.page_source= driver.title
命令行工具
curl命令
# 下载整个页面 curl -o page.html https://example.com # 查看响应头 curl -I https://example.com
wget命令
# 下载页面 wget https://example.com # 下载整个网站 wget -r -l 1 https://example.com
浏览器扩展工具
- SingleFile:保存完整页面
- Web Scraper:可视化数据提取
- Data Scraper:表格数据抓取
- Page Ruler:测量页面元素
API方式
// 使用Fetch API
fetch('https://example.com')
.then(response => response.text())
.then(html => console.log(html))
实际应用场景
提取表格数据
import pandas as pd
tables = pd.read_html('https://example.com')
df = tables[0] # 第一个表格
// 提取文章正文(配合Readability) const article = new Readability(document.cloneNode(true)).parse() console.log(article.textContent)
注意事项
- 遵守robots.txt:尊重网站的爬取规则
- 请求频率控制:避免高频请求
- 用户代理设置:模拟真实浏览器
- 处理反爬机制:可能需要cookies或headers
你想提取什么类型的页面?我可以提供更具体的代码示例。
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。