网络爬虫(Web Crawler)是一种自动从互联网上抓取数据的程序。各种计算机语言基本都支持爬虫操作,如node.js、java、bash、powershell等等,其中,Python因其丰富的库和简洁的语法成为编写爬虫的热门选择。
一、基础概念
工作原理:
- 发送HTTP请求获取网页内容
- 解析响应内容(HTML/JSON/XML等)
- 提取所需数据
- 存储数据(文件/数据库)
- (可选)跟踪链接继续爬取
类型:
- 通用爬虫:如搜索引擎爬虫
- 聚焦爬虫:针对特定网站/内容
- 增量式爬虫:只爬取更新内容
- 深层爬虫:处理需要登录/交互的页面
二、python爬虫核心库
1、请求库
1.1、requests:pip install requests
import requests
response = requests.get('https://example.com')
print(response.text)
1.2、Python内置库:Urllib
from urllib.request import urlopen
html = urlopen('https://example.com').read()
1.3、异步:aiohttp (pip install aiohttp aiolimiter)
<