目录
- Python进阶教程:网络编程与数据抓取
- 一、HTTP 基础
- 二、urllib:标准库 HTTP 客户端
- 三、requests:更优雅的 HTTP 库
- 四、HTML 解析:BeautifulSoup
- 五、实战:抓取网页文章标题
- 六、爬虫的注意事项
- 七、进阶:接口调用(API)
- 总结
Python进阶教程:网络编程与数据抓取
本文是Python 入门教程系列的第 5 篇。前面四篇介绍了基础语法、OOP、文件操作、常用标准库,本篇介绍网络编程与数据抓取(爬虫基础)。
一、HTTP 基础
网络编程的核心是 HTTP 协议。HTTP 请求主要由四部分组成:
- 方法:GET(获取)、POST(提交)、PUT、DELETE 等
- URL:资源地址
- 请求头:User-Agent、Cookie、Content-Type 等
- 请求体:POST 时携带的数据
响应同样包含状态码(200 成功、404 不存在、500 服务器错误)、响应头和响应体。
二、urllib:标准库 HTTP 客户端
importurllib.requestimporturllib.parse# GET 请求url="https://httpbin.org/get"req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0"})withurllib.request.urlopen(req,timeout=10)asresp:print(resp.status)# 200print(resp.read().decode("utf-8")[:200])# POST 请求data=urllib.parse.urlencode({"name":"Alice","age":20}).encode()req=urllib.request.Request("https://httpbin.org/post",data=data)withurllib.request.urlopen(req)asresp:print(resp.read().decode("utf-8")[:200])三、requests:更优雅的 HTTP 库
requests 是第三方库(pip install requests),是实际开发中的首选:
importrequests# GET 请求resp=requests.get("https://httpbin.org/get",params={"q":"python"},timeout=10)print(resp.status_code)# 200print(resp.json())# 自动解析 JSON# POST 请求resp=requests.post("https://httpbin.org/post",json={"name":"Alice"})print(resp.json())# 自定义请求头(模拟浏览器)headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}resp=requests.get("https://httpbin.org/headers",headers=headers)# 下载文件resp=requests.get("https://httpbin.org/image/png",stream=True)withopen("image.png","wb")asf:forchunkinresp.iter_content(chunk_size=8192):f.write(chunk)四、HTML 解析:BeautifulSoup
抓取网页后需要解析 HTML,BeautifulSoup 是最常用的工具(pip install beautifulsoup4):
frombs4importBeautifulSoupimportrequests html=""" <html><body> <h1>Python 教程</h1> <div class="article"> <a href="/p1">第一篇</a> <a href="/p2">第二篇</a> </div> </body></html> """soup=BeautifulSoup(html,"html.parser")# 获取标题print(soup.h1.text)# Python 教程# 按 class 查找div=soup.find("div",class_="article")# 查找所有链接foraindiv.find_all("a"):print(a.text,a["href"])# 第一篇 /p1# 第二篇 /p2五、实战:抓取网页文章标题
综合运用以上知识,写一个抓取网页所有链接和标题的小工具:
importrequestsfrombs4importBeautifulSoupdeffetch_links(url):"""抓取页面中所有链接及其文本"""try:headers={"User-Agent":"Mozilla/5.0"}resp=requests.get(url,headers=headers,timeout=10)resp.raise_for_status()# 非 200 会抛出异常soup=BeautifulSoup(resp.text,"html.parser")links=[]forainsoup.find_all("a",href=True):text=a.text.strip()or"(无文本)"links.append((text[:30],a["href"]))returnlinksexceptrequests.RequestExceptionase:print(f"请求失败:{e}")return[]# 使用示例url="https://example.com"fortext,hrefinfetch_links(url)[:10]:print(f"{text}->{href}")六、爬虫的注意事项
合法、规范的爬虫需要注意:
- 遵守 robots.txt:访问站点前检查
https://站点/robots.txt了解允许爬取的内容。 - 控制请求频率:用 time.sleep 间隔请求,避免给服务器造成压力。
- 设置合理 UA:识别为真实浏览器,但不要伪装成他人。
- 尊重版权:只抓取允许的数据,注意使用条款。
- 反爬处理:遇到验证码、登录墙时不要强行绕过。
importtimeimportrequests urls=["https://httpbin.org/get"]*5forurlinurls:resp=requests.get(url,timeout=10)print(resp.status_code)time.sleep(2)# 每 2 秒请求一次,礼貌抓取七、进阶:接口调用(API)
现代开发更多是调用 API 获取 JSON 数据,配合上篇的 json 库非常方便:
importrequestsimportjsondefcall_api(url,params=None):resp=requests.get(url,params=params,timeout=10)ifresp.status_code==200:returnresp.json()else:print(f"API 返回错误:{resp.status_code}")returnNone# 调用公开 API 获取天气信息(示例)data=call_api("https://httpbin.org/json")ifdata:print(json.dumps(data,ensure_ascii=False,indent=2))总结
本篇介绍了 HTTP 基础、urllib 与 requests 两种 HTTP 客户端、BeautifulSoup 网页解析、以及爬虫的规范与注意事项,并提供了两个实战工具。下一篇将介绍多线程与多进程,敬请期待!