超详细 Python 爬虫指南
目录
一、爬虫的基本原理
- HTTP 请求与响应
- 爬虫通过
HTTP协议与目标网站服务器通信。 - 发送请求时可指定
URL、请求方法(GET或POST)、请求头等。 - 服务器根据请求返回 HTML 页面、JSON 数据或其他格式的响应。
- 爬虫通过
- HTML 解析
HTML 是网页的主要结构。爬虫通过解析 HTML 提取有用信息,如标题、图片、表格等。 - 数据存储
抓取的数据可存储到文件(如 CSV、JSON)、数据库(如 MySQL、MongoDB)等介质中,便于后续分析。 - 反爬机制
- User-Agent 检测:服务器检查请求来源是否合法。
- 频率限制:高频访问可能触发封禁。
- 验证码验证:部分网站通过验证码阻止自动化行为。
- robots.txt 协议
网站通过robots.txt指定哪些页面可以被爬取,爬虫需遵守此协议。
二、爬虫实现步骤
1. 准备工作
安装必要的库:
pip install requests beautifulsoup4 lxml pandas 2. 详细代码实现
(1)发送 HTTP 请求 通过 requests 库获取网页内容。
import requests # 定义目标 URL url = "https://example.com" # 设置请求头,伪装为浏览器访问 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" } # 发送请求 response = requests.get(url, headers=headers) # 检查状态码 if response.status_code == 200: print("请求成功!") print(response.text[:500]) # 打印部分网页内容 else: print(f"请求失败,状态码: {response.status_code}") (2)解析 HTML 数据 使用 BeautifulSoup 提取 HTML 中的内容。
from bs4 import BeautifulSoup # 使用 BeautifulSoup 解析 HTML soup = BeautifulSoup(response.text, "lxml") # 提取网页标题 title = soup.title.string print(f"网页标题: {title}") # 提取所有超链接 links = [] for a_tag in soup.find_all("a", href=True): links.append(a_tag["href"]) print("提取到的链接:") print(" ".join(links)) (3)存储数据 将数据保存为 CSV 文件。
import pandas as pd # 构造数据字典 data = {"Links": links} # 转换为 DataFrame df = pd.DataFrame(data) # 保存为 CSV df.to_csv("links.csv", index=False, encoding="utf-8-sig") print("数据已保存到 links.csv") (4)动态网页处理 有些网页通过 JavaScript 加载数据,requests 无法直接抓取。这时需使用浏览器自动化工具,如 Selenium 或 Playwright。
以下是 Selenium 的示例:
pip install selenium from selenium import webdriver from selenium.webdriver.common.by import By # 配置 Selenium WebDriver(以 Chrome 为例) options = webdriver.ChromeOptions() options.add_argument("--headless") # 无头模式 driver = webdriver.Chrome(options=options) # 打开网页 driver.get("https://example.com") # 等待页面加载 driver.implicitly_wait(10) # 提取动态加载的内容 titles = driver.find_elements(By.TAG_NAME, "h1") for title in titles: print(title.text) # 关闭浏览器 driver.quit() 三、处理反爬机制
添加随机延迟 避免频繁请求被封禁:
import time import random time.sleep(random.uniform(1, 3)) # 随机延迟 1-3 秒 使用代理 IP 通过代理绕过 IP 封禁:
proxies = { "http": "http://username:password@proxyserver:port", "https": "http://username:password@proxyserver:port" } response = requests.get(url, headers=headers, proxies=proxies) 处理验证码 使用 OCR 识别验证码:
pip install pytesseract pillow from PIL import Image import pytesseract # 读取验证码图片 image = Image.open("captcha.png") # 使用 OCR 识别文本 captcha_text = pytesseract.image_to_string(image) print(f"验证码内容: {captcha_text}") 四、爬取复杂数据的技巧
1. JSON 数据爬取
许多网站的动态内容通过 API 提供 JSON 数据,可以直接请求这些接口:
api_url = "https://example.com/api/data" response = requests.get(api_url, headers=headers) # 解析 JSON 数据 data = response.json() print(data) 2. 分页数据爬取
自动抓取多页内容:
base_url = "https://example.com/page={}" for page in range(1, 6): url = base_url.format(page) response = requests.get(url, headers=headers) print(f"抓取第 {page} 页内容") 3. 下载文件
下载图片或文件到本地:
file_url = "https://example.com/image.jpg" response = requests.get(file_url, stream=True) # 保存到本地 with open("image.jpg", "wb") as file: for chunk in response.iter_content(chunk_size=1024): file.write(chunk) print("文件下载完成!") 五、完整爬虫示例
以下是一个完整的爬虫脚本,抓取新闻网站标题与链接并保存为 CSV 文件:
import requests from bs4 import BeautifulSoup import pandas as pd import time import random # 设置目标 URL 和请求头 base_url = "https://news.ycombinator.com/" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" } # 存储数据 titles = [] links = [] # 爬取内容 for page in range(1, 4): # 抓取前三页 url = f"{base_url}?p={page}" response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, "lxml") for item in soup.find_all("a", class_="titlelink"): titles.append(item.text) links.append(item["href"]) print(f"完成第 {page} 页爬取") time.sleep(random.uniform(1, 3)) # 随机延迟 # 保存数据到 CSV data = {"Title": titles, "Link": links} df = pd.DataFrame(data) df.to_csv("news.csv", index=False, encoding="utf-8-sig") print("新闻数据已保存到 news.csv") 六、注意事项
- 避免法律风险
- 爬取前阅读目标网站的使用条款。
- 遵守
robots.txt协议。
- 优化性能
使用多线程或异步技术(如asyncio、aiohttp)提高效率。 - 应对反爬
熟练使用代理、延迟和伪装技巧。