Python入门到脱坑案例:简单网页爬虫
网页爬虫是Python的一个非常实用的应用场景。下面我将介绍一个适合初学者的简单爬虫案例,使用Python的requests和BeautifulSoup库来抓取网页内容。
准备工作
首先需要安装必要的库:
pip install requests beautifulsoup4
案例1:获取网页标题和所有链接
import requests
from bs4 import BeautifulSoup
def simple_crawler(url):
try:
# 发送HTTP请求
response = requests.get(url)
response.raise_for_status() # 检查请求是否成功
# 解析HTML内容
soup = BeautifulSoup(response.text, 'html.parser')
# 获取网页标题
title = soup.title.string
print(f"网页标题: {title}")
# 获取所有链接
print("\n网页中的链接:")
for link in soup.find_all('a'):
href = link.get('href')
if href and href.startswith('http'): # 只显示完整的URL
print(href)
except requests.exceptions.RequestException as e:
print(f"请求出错: {e}")
# 使用示例
url = input("请输入要爬取的网页URL: ")
simple_crawler(url)
案例2:抓取特定内容(例如新闻标题)
import requests
from bs4 import BeautifulSoup
def news_crawler():
url = "https://news.baidu.com/"
try:
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
news_titles = soup.select('.hotnews a') # 根据实际网页结构调整选择器
print("百度热点新闻:")
for i, title in enumerate(news_titles, 1):
print(f"{i}. {title.get_text(strip=True)}")
except Exception as e:
print(f"发生错误: {e}")
news_crawler()
案例3:简单的图片下载器
import requests
from bs4 import BeautifulSoup
import os
def image_downloader(url, save_dir='images'):
try:
# 创建保存目录
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# 获取网页内容
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
img_tags = soup.find_all('img')
print(f"找到 {len(img_tags)} 张图片,开始下载...")
for i, img in enumerate(img_tags):
img_url = img.get('src')
if img_url and img_url.startswith('http'):
try:
img_data = requests.get(img_url).content
with open(f"{save_dir}/image_{i+1}.jpg", 'wb') as f:
f.write(img_data)
print(f"已下载: image_{i+1}.jpg")
except:
continue
print("下载完成!")
except Exception as e:
print(f"发生错误: {e}")
# 使用示例
url = input("请输入包含图片的网页URL: ")
image_downloader(url)
案例4:简单的豆瓣电影Top250爬虫
import requests
from bs4 import BeautifulSoup
import csv
def douban_top250():
base_url = "https://movie.douban.com/top250"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
movies = []
for start in range(0, 250, 25):
url = f"{base_url}?start={start}"
try:
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
for item in soup.select('.item'):
title = item.select_one('.title').get_text(strip=True)
rating = item.select_one('.rating_num').get_text(strip=True)
movies.append((title, rating))
except Exception as e:
print(f"获取数据出错: {e}")
# 保存到CSV文件
with open('douban_top250.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow(['电影名称', '评分'])
writer.writerows(movies)
print("数据已保存到 douban_top250.csv")
douban_top250()
爬虫注意事项
- 遵守robots.txt:检查目标网站的robots.txt文件,了解哪些内容允许爬取
- 设置请求头:添加User-Agent模拟浏览器访问
- 限制请求频率:避免对服务器造成过大压力
- import time time.sleep(1) # 每次请求间隔1秒
- 错误处理:网络请求可能失败,需要妥善处理异常
- 合法性:确保你的爬虫用途合法,不违反网站的使用条款
总结
这个简单的爬虫案例涵盖了:
- 使用requests库发送HTTP请求
- 使用BeautifulSoup解析HTML内容
- 提取特定元素和数据
- 简单的数据存储(CSV文件)
- 基本的错误处理
对于初学者来说,这些案例可以帮助理解网页爬虫的基本原理和Python的相关库使用。随着学习的深入,可以进一步学习更高级的爬虫框架如Scrapy,以及处理JavaScript渲染的页面等技术。