Python 爬虫案例

以下是一些常见的 Python 爬虫案例,涵盖了不同的应用场景和技术点:

  1. 简单网页内容爬取

案例:爬取网页标题和简介

import requests

from bs4 import BeautifulSoup

url = "https://www.runoob.com/"

response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')

title = soup.title.string

description = soup.find('meta', attrs={'name': 'description'})'content'

print(f"标题: {title}")

print(f"简介: {description}")

  1. 爬取图片

案例:爬取图片网站并下载图片

import os

import requests

from bs4 import BeautifulSoup

url = "https://unsplash.com/s/photos/nature"

response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')

创建文件夹存储图片

if not os.path.exists('images'):

os.makedirs('images')

找到所有图片标签

img_tags = soup.find_all('img')

for idx, img in enumerate(img_tags):

img_url = img'src'

下载图片

img_data = requests.get(img_url).content

with open(f'images/img_{idx}.jpg', 'wb') as handler:

handler.write(img_data)

  1. 爬取数据并存储

案例:爬取豆瓣电影 Top250 并存储到 CSV

import csv

import requests

from bs4 import BeautifulSoup

url = "https://movie.douban.com/top250"

response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')

movies = \[\]

for item in soup.select('.item'):

title = item.select('.title')0.get_text()

rating = item.select('.rating_num')0.get_text()

director = item.select('.bd p')0.get_text().split('\n')1.strip().split('/')0

movies.append(title, rating, director)

写入 CSV 文件

with open('douban_top250.csv', 'w', newline='', encoding='utf-8') as f:

writer = csv.writer(f)

writer.writerow('标题', '评分', '导演')

writer.writerows(movies)

  1. 动态网页爬取

案例:使用 Selenium 爬取动态加载的网页

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

import time

启动浏览器

driver = webdriver.Chrome()

driver.get("https://www.jd.com")

搜索商品

search_box = driver.find_element(By.ID, 'key')

search_box.send_keys('笔记本电脑')

search_box.send_keys(Keys.RETURN)

time.sleep(3) # 等待页面加载

获取商品列表

products = driver.find_elements(By.CLASS_NAME, 'gl-item')

for product in products:

try:

name = product.find_element(By.CLASS_NAME, 'p-name').text

price = product.find_element(By.CLASS_NAME, 'p-price').text

print(f"商品: {name}, 价格: {price}")

except Exception as e:

print(e)

driver.quit()

  1. API 数据爬取

案例:爬取 GitHub API 数据

import requests

获取 Python 仓库信息

url = "https://api.github.com/search/repositories?q=language:python\&sort=stars"

response = requests.get(url)

data = response.json()

for item in data'items':

name = item'name'

description = item'description'

stars = item'stargazers_count'

print(f"仓库: {name}, 描述: {description}, 星数: {stars}")

  1. 爬取登录后的数据

案例:模拟登录并爬取数据

import requests

login_url = "https://example.com/login"

data_url = "https://example.com/dashboard"

登录信息

payload = {

'username': 'your_username',

'password': 'your_password'

}

使用会话保持登录状态

with requests.Session() as session:

发送登录请求

session.post(login_url, data=payload)

访问需要登录的页面

response = session.get(data_url)

print(response.text)

注意事项

  1. 遵守网站规则:在爬取之前,查看目标网站的 robots.txt 文件,了解哪些页面允许爬取。

  2. 设置合理的请求间隔:避免频繁请求导致服务器压力过大或被封禁。

  3. 处理反爬机制:如果遇到反爬,可以尝试使用代理 IP、设置请求头(User-Agent)等方法。

  4. 合法性:确保爬取的数据和行为符合法律法规。

这些案例可以帮助你快速上手 Python 爬虫开发,根据实际需求选择合适的技术和工具。

相关推荐
copyer_xyf2 分钟前
FastAPI 项目骨架搭建
前端·后端·python
十正3 分钟前
aiohttp.TCPConnector 连接池原理详解
网络·python·tcp·aiohttp
LoserChaser6 分钟前
Flask 文件上传服务器 - 知识点总结
服务器·python·flask
cd988808 分钟前
2026年,哪家电销机器人定制更灵活?
python
二十七剑10 分钟前
LangGraph 源码深度解析:_branch.py 条件分支底层实现原理
python
KaMeidebaby16 分钟前
卡梅德生物技术快报|噬菌体展示文库构建全流程解析 | 大豆球蛋白纳米抗体筛选实践
人工智能·python·tcp/ip·算法·机器学习
傻啦嘿哟19 分钟前
为什么Python没有块级作用域?
开发语言·python
CC数学建模24 分钟前
2026年第十六届APMCM 亚太地区大学生数学建模竞赛(中文赛项)赛题B题:高性能芯片热管理系统的优化问题完整思路、代码、模型、文章,全网首发高质量分享!
python·算法·数学建模
Maydaycxc26 分钟前
Python 实现 RPA + AI 自动化:大模型 OCR + 网页操作完整源码实战
人工智能·python·opencv·selenium·自动化·ocr·rpa
stephon_10030 分钟前
从零设计 Agent 上下文压缩:三级流水线与动态阈值,治好 context too long(附开源实现)
人工智能·python