30天学会Python编程:20. Python网络爬虫简介
wptr33 2025-07-08 23:41 64 浏览
20.1 网络爬虫基础
20.1.1 爬虫定义与原理
20.1.2 法律与道德规范
表19-1 爬虫合法性要点
注意事项 | 说明 | 合规建议 |
robots协议 | 网站访问规则 | 遵守robots.txt |
访问频率 | 请求间隔控制 | 添加适当延迟 |
数据使用 | 版权与隐私 | 仅用于合法用途 |
用户认证 | 登录权限 | 不破解验证机制 |
20.2 请求库使用
20.2.1 requests库
基本使用:
import requests
def fetch_page(url):
try:
response = requests.get(
url,
headers={
'User-Agent': 'Mozilla/5.0',
'Accept-Language': 'zh-CN'
},
timeout=5
)
response.raise_for_status() # 检查HTTP状态码
return response.text
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
return None
# 示例使用
html = fetch_page('https://example.com')
20.2.2 高级请求技巧
# 会话保持
session = requests.Session()
session.get('https://example.com/login', params={'user': 'test'})
# 代理设置
proxies = {
'http': 'http://10.10.1.10:3128',
'https': 'http://10.10.1.10:1080'
}
response = requests.get(url, proxies=proxies)
# 文件下载
with requests.get('https://example.com/image.jpg', stream=True) as r:
with open('image.jpg', 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
20.3 数据解析技术
20.3.1 BeautifulSoup解析
from bs4 import BeautifulSoup
def parse_html(html):
soup = BeautifulSoup(html, 'lxml')
# CSS选择器
titles = soup.select('h1.article-title')
# 属性提取
links = [a['href'] for a in soup.find_all('a', class_='external')]
# 文本处理
content = soup.find('div', id='content').get_text(strip=True, separator='\n')
return {
'titles': [t.text for t in titles],
'links': links,
'content': content
}
20.3.2 XPath与lxml
from lxml import etree
def xpath_parse(html):
tree = etree.HTML(html)
# 提取商品价格
prices = tree.xpath('//div[@class="price"]/text()')
# 提取嵌套数据
items = []
for item in tree.xpath('//div[@class="product"]'):
items.append({
'name': item.xpath('.//h2/text()')[0],
'sku': item.xpath('./@data-sku')[0]
})
return {'prices': prices, 'items': items}
20.4 动态页面处理
20.4.1 Selenium自动化
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def selenium_crawl(url):
options = webdriver.ChromeOptions()
options.add_argument('--headless') # 无头模式
driver = webdriver.Chrome(options=options)
try:
driver.get(url)
# 等待元素加载
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".dynamic-content"))
)
# 执行JavaScript
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# 获取渲染后页面
html = driver.page_source
return html
finally:
driver.quit()
20.4.2 接口逆向分析
import json
def api_crawl():
# 分析XHR请求
api_url = 'https://api.example.com/data'
params = {
'page': 1,
'size': 20,
'timestamp': int(time.time()*1000)
}
response = requests.get(api_url, params=params)
data = response.json()
# 解析JSON数据
for item in data['list']:
print(f"商品: {item['name']}, 价格: {item['price']}")
20.5 数据存储方案
20.5.1 文件存储
import csv
import json
def save_to_csv(data, filename):
with open(filename, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
def save_to_json(data, filename):
with open(filename, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
20.5.2 数据库存储
import sqlite3
import pymongo
# SQLite存储
def sqlite_save(data):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS products
(id TEXT, name TEXT, price REAL)''')
c.executemany('INSERT INTO products VALUES (?,?,?)',
[(d['id'], d['name'], d['price']) for d in data])
conn.commit()
# MongoDB存储
def mongo_save(data):
client = pymongo.MongoClient('mongodb://localhost:27017/')
db = client['web_data']
collection = db['products']
collection.insert_many(data)
20.6 反爬应对策略
20.6.1 常见反爬机制
表19-2 常见反爬技术与应对
反爬技术 | 识别特征 | 破解方法 |
User-Agent检测 | 无浏览器特征 | 轮换User-Agent |
IP限制 | 频繁访问被封 | 使用代理IP池 |
验证码 | 出现验证页面 | 打码平台/OCR识别 |
请求参数加密 | 参数含加密字段 | 逆向JS分析 |
动态渲染 | 数据通过JS加载 | Selenium/Puppeteer |
20.6.2 高级反反爬技巧
# 代理IP池示例
class ProxyPool:
def __init__(self):
self.proxies = [
'http://ip1:port',
'http://ip2:port',
# ...
]
self.current = 0
def get_proxy(self):
proxy = self.proxies[self.current % len(self.proxies)]
self.current += 1
return {'http': proxy, 'https': proxy}
# 请求头随机生成
from fake_useragent import UserAgent
ua = UserAgent()
def get_random_headers():
return {
'User-Agent': ua.random,
'Referer': 'https://www.google.com/',
'Accept-Encoding': 'gzip, deflate, br'
}
20.7 应用举例
案例1:电商商品爬虫
import requests
from bs4 import BeautifulSoup
import time
import random
def ecommerce_crawler(base_url, max_page=10):
products = []
for page in range(1, max_page+1):
# 带延迟的请求
time.sleep(random.uniform(1, 3))
url = f"{base_url}?page={page}"
html = fetch_page(url)
if not html:
continue
soup = BeautifulSoup(html, 'lxml')
items = soup.select('.product-item')
for item in items:
try:
products.append({
'name': item.select_one('.name').text.strip(),
'price': float(item.select_one('.price').text.replace('yen', '')),
'sku': item['data-sku'],
'rating': item.select_one('.rating').text.strip()
})
except Exception as e:
print(f"解析失败: {e}")
save_to_csv(products, 'products.csv')
return products
# 使用示例
ecommerce_crawler('https://example.com/products')
案例2:新闻聚合爬虫
import schedule
import datetime
def news_monitor():
sources = [
'https://news.source1.com/rss',
'https://news.source2.com/api/latest'
]
all_news = []
for url in sources:
try:
if 'rss' in url:
# 解析RSS
news = parse_rss(url)
else:
# 调用API
news = parse_news_api(url)
all_news.extend(news)
except Exception as e:
print(f"爬取失败 {url}: {e}")
# 去重存储
store_news(all_news)
print(f"{datetime.datetime.now()} 已抓取{len(all_news)}条新闻")
# 定时任务
schedule.every(1).hours.do(news_monitor)
while True:
schedule.run_pending()
time.sleep(60)
20.8 知识图谱
20.9 学习总结
核心要点:
- 掌握HTTP请求与响应处理
- 熟练使用主流解析工具
- 理解动态页面加载原理
- 能够应对常见反爬措施
实践建议:
- 遵守爬虫道德规范
- 添加随机请求延迟
- 实现异常处理机制
- 定期维护代理池
进阶方向:
- 分布式爬虫架构
- 验证码智能识别
- 数据清洗与分析
- 反爬JS逆向工程
常见陷阱:
- 触发网站防护机制
- 页面结构变更导致解析失败
- 未处理编码问题
- 法律风险意识不足
持续更新Python编程学习日志与技巧,敬请关注!
#编程# #学习# #python# #在头条记录我的2025#
相关推荐
- oracle数据导入导出_oracle数据导入导出工具
-
关于oracle的数据导入导出,这个功能的使用场景,一般是换服务环境,把原先的oracle数据导入到另外一台oracle数据库,或者导出备份使用。只不过oracle的导入导出命令不好记忆,稍稍有点复杂...
- 继续学习Python中的while true/break语句
-
上次讲到if语句的用法,大家在微信公众号问了小编很多问题,那么小编在这几种解决一下,1.else和elif是子模块,不能单独使用2.一个if语句中可以包括很多个elif语句,但结尾只能有一个...
- python continue和break的区别_python中break语句和continue语句的区别
-
python中循环语句经常会使用continue和break,那么这2者的区别是?continue是跳出本次循环,进行下一次循环;break是跳出整个循环;例如:...
- 简单学Python——关键字6——break和continue
-
Python退出循环,有break语句和continue语句两种实现方式。break语句和continue语句的区别:break语句作用是终止循环。continue语句作用是跳出本轮循环,继续下一次循...
- 2-1,0基础学Python之 break退出循环、 continue继续循环 多重循
-
用for循环或者while循环时,如果要在循环体内直接退出循环,可以使用break语句。比如计算1至100的整数和,我们用while来实现:sum=0x=1whileTrue...
- Python 中 break 和 continue 傻傻分不清
-
大家好啊,我是大田。...
- python中的流程控制语句:continue、break 和 return使用方法
-
Python中,continue、break和return是控制流程的关键语句,用于在循环或函数中提前退出或跳过某些操作。它们的用途和区别如下:1.continue(跳过当前循环的剩余部分,进...
- L017:continue和break - 教程文案
-
continue和break在Python中,continue和break是用于控制循环(如for和while)执行流程的关键字,它们的作用如下:1.continue:跳过当前迭代,...
- 作为前端开发者,你都经历过怎样的面试?
-
已经裸辞1个月了,最近开始投简历找工作,遇到各种各样的面试,今天分享一下。其实在职的时候也做过面试官,面试官时,感觉自己问的问题很难区分候选人的能力,最好的办法就是看看候选人的github上的代码仓库...
- 面试被问 const 是否不可变?这样回答才显功底
-
作为前端开发者,我在学习ES6特性时,总被const的"善变"搞得一头雾水——为什么用const声明的数组还能push元素?为什么基本类型赋值就会报错?直到翻遍MDN文档、对着内存图反...
- 2023金九银十必看前端面试题!2w字精品!
-
导文2023金九银十必看前端面试题!金九银十黄金期来了想要跳槽的小伙伴快来看啊CSS1.请解释CSS的盒模型是什么,并描述其组成部分。...
- 前端面试总结_前端面试题整理
-
记得当时大二的时候,看到实验室的学长学姐忙于各种春招,有些收获了大厂offer,有些还在苦苦面试,其实那时候的心里还蛮忐忑的,不知道自己大三的时候会是什么样的一个水平,所以从19年的寒假放完,大二下学...
- 由浅入深,66条JavaScript面试知识点(七)
-
作者:JakeZhang转发链接:https://juejin.im/post/5ef8377f6fb9a07e693a6061目录...
- 2024前端面试真题之—VUE篇_前端面试题vue2020及答案
-
添加图片注释,不超过140字(可选)...
- 今年最常见的前端面试题,你会做几道?
-
在面试或招聘前端开发人员时,期望、现实和需求之间总是存在着巨大差距。面试其实是一个交流想法的地方,挑战人们的思考方式,并客观地分析给定的问题。可以通过面试了解人们如何做出决策,了解一个人对技术和解决问...
- 一周热门
- 最近发表
-
- oracle数据导入导出_oracle数据导入导出工具
- 继续学习Python中的while true/break语句
- python continue和break的区别_python中break语句和continue语句的区别
- 简单学Python——关键字6——break和continue
- 2-1,0基础学Python之 break退出循环、 continue继续循环 多重循
- Python 中 break 和 continue 傻傻分不清
- python中的流程控制语句:continue、break 和 return使用方法
- L017:continue和break - 教程文案
- 作为前端开发者,你都经历过怎样的面试?
- 面试被问 const 是否不可变?这样回答才显功底
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)
