Python 爬虫常用库总结与进阶指南

Python 爬虫常用库总结与进阶指南

文章目录

[1 基础中的基础](#1 基础中的基础)

一、Python环境配置

[1.1 Python环境安装](#1.1 Python环境安装)

[1.2 验证Python环境](#1.2 验证Python环境)

二、安装第三方库

[2.1 pip工具](#2.1 pip工具)

[2.2 安装requests和BeautifulSoup库](#2.2 安装requests和BeautifulSoup库)

三、第一个爬虫程序

[3.1 导入库](#3.1 导入库)

[3.2 发送请求并获取网页内容](#3.2 发送请求并获取网页内容)

[3.3 解析网页内容并提取信息](#3.3 解析网页内容并提取信息)

[2 获取到信息的处理方法](#2 获取到信息的处理方法)

解析库

BeautifulSoup库入门

安装

使用示例

lxml库入门

安装

使用示例

正则表达式使用

安装

使用示例

[3 处理后数据的处理方法](#3 处理后数据的处理方法)

数据存储库

文件存储

[数据库存储(SQLite, MySQL)](#数据库存储(SQLite, MySQL))

数据持久化

[4 进阶指南](#4 进阶指南)

目录

异步库

多线程爬取

协程爬取

异步IO

反爬策略及应对

User-Agent伪装

IP代理使用

验证码处理

[5 小结与其他](#5 小结与其他)

[1. 基础库介绍](#1. 基础库介绍)

[2. 模拟登录](#2. 模拟登录)

[2.1 使用 requests 处理 cookies](#2.1 使用 requests 处理 cookies)

[2.2 使用 Selenium 进行模拟登录](#2.2 使用 Selenium 进行模拟登录)

[3. 动态网页爬取(Selenium)](#3. 动态网页爬取(Selenium))

[4. 分布式爬虫](#4. 分布式爬虫)

1 基础中的基础

本教程将介绍Python爬虫的基础知识,包括Python环境配置,安装第三方库,以及编写第一个爬虫程序。

一、Python环境配置

Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。首先,我们需要配置Python环境。你可以从Python官网下载最新的Python版本并安装。

1.1 Python环境安装

访问Python官网下载页面,选择适合你操作系统的Python版本进行下载和安装。

1.2 验证Python环境

安装完成后,打开命令行工具,输入以下命令来验证Python是否安装成功:

python

复制代码

python --version

如果显示出Python的版本号,说明Python已经成功安装。

二、安装第三方库

Python有许多强大的第三方库可以帮助我们进行爬虫开发,其中最常用的是requests和BeautifulSoup。我们可以使用pip工具来安装这些库。

2.1 pip工具

pip是Python的包管理器,可以用来安装和管理Python库。如果你的Python环境中还没有pip,你可以从这里下载并安装。

2.2 安装requests和BeautifulSoup库

在命令行中输入以下命令来安装requests和BeautifulSoup库:

python

复制代码

pip install requests beautifulsoup4

三、第一个爬虫程序

接下来,我们将编写一个简单的爬虫程序,用于抓取网页上的信息。

3.1 导入库

首先,我们需要导入requests和BeautifulSoup库:

python

复制代码

import requests

from bs4 import BeautifulSoup

3.2 发送请求并获取网页内容

然后,我们可以使用requests库的get方法来发送一个HTTP请求,获取网页的内容:

python

复制代码

url = 'http://example.com' # 需要爬取的网页URL

response = requests.get(url) # 发送GET请求

html_content = response.text # 获取网页内容

3.3 解析网页内容并提取信息

最后,我们可以使用BeautifulSoup库来解析网页内容,并提取出我们需要的信息:

python

复制代码

soup = BeautifulSoup(html_content, 'html.parser') # 创建BeautifulSoup对象

title = soup.title.string # 提取网页标题

print(title) # 打印网页标题

2 获取到信息的处理方法

解析库

BeautifulSoup库入门

BeautifulSoup是一个Python的HTML或XML的解析库,用于从网页中提取数据。它通常与requests和lxml等库一起使用,以获取和解析网页内容。

安装

python

复制代码

pip install beautifulsoup4

使用示例

python

复制代码

from bs4 import BeautifulSoup

import requests

url = 'http://example.com'

response = requests.get(url)

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

lxml库入门

lxml是Python的一个高性能的库,用于处理XML和HTML。它提供了简单易用的API,可以快速地解析和操作XML和HTML文档。

安装

python

复制代码

pip install lxml

使用示例

python

复制代码

from lxml import etree

import requests

url = 'http://example.com'

response = requests.get(url)

tree = etree.HTML(response.text)

正则表达式使用

正则表达式是一种强大的文本匹配工具,可以用来匹配、查找、替换字符串中的特定模式。在Python中,可以使用re模块来处理正则表达式。

安装

无需安装,re模块是Python的标准库之一。

使用示例

python

复制代码

import re

pattern = r'\d+' # 匹配一个或多个数字的正则表达式

text = 'abc123def456'

result = re.findall(pattern, text) # 返回所有匹配的结果列表:['123', '456']

3 处理后数据的处理方法

数据存储库

文件存储

在Python中,我们可以使用内置的open()函数来读写文件。以下是一个简单的例子:

python

复制代码

# 写入文件

with open('test.txt', 'w') as f:

f.write('Hello, World!')

# 读取文件

with open('test.txt', 'r') as f:

print(f.read())

数据库存储(SQLite, MySQL)

对于数据库存储,我们可以使用SQLite和MySQL。以下是一个使用SQLite的例子:

python

复制代码

import sqlite3

# 连接到SQLite数据库

# 数据库文件是test.db

# 如果文件不存在,会自动在当前目录创建:

conn = sqlite3.connect('test.db')

# 创建一个Cursor:

cursor = conn.cursor()

# 执行一条SQL语句,创建user表:

cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')

# 继续执行一条SQL语句,插入一条记录:

cursor.execute('insert into user (id, name) values (\'1\', \'Michael\')')

# 通过rowcount获得插入的行数:

print(cursor.rowcount)

# 关闭Cursor:

cursor.close()

# 提交事务:

conn.commit()

# 关闭Connection:

conn.close()

对于MySQL,我们需要使用pymysql库:

python

复制代码

import pymysql

# 连接数据库

connection = pymysql.connect(host='localhost', user='root', password='password', database='test')

try:

with connection.cursor() as cursor:

# 创建一个新的记录

sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"

cursor.execute(sql, ('webmaster@python.org', 'very-secret'))

connection.commit()

finally:

connection.close()

数据持久化

数据持久化是将内存中的数据保存到可永久存储的设备中。在Python中,我们通常使用pickle库来实现数据的序列化和反序列化。以下是一个例子:

python

复制代码

import pickle

data = {'a': [1, 2.0], 'b': ('string', u'Unicode string'), 'c': None}

binary_data = pickle.dumps(data) # Pickling data, also converting numpy array to bytes.

print(binary_data) # b'\x80x04\x95x0b\x00x00\x00x00\x00x00\x00]\x94(\x8c\x08key\x94\x8c

class\x94K\x01u.'

print(type(binary_data)) #

data2 = pickle.loads(binary_data) # Unpickling data. Also deserializing a numpy array from bytes.

print(data2) # {'a': [1, 2], 'b': ('string', u'Unicode string'), 'c': None}

4 进阶指南

目录

异步库

多线程爬取

协程爬取

异步IO

异步库

多线程爬取

在Python中,可以使用threading模块来实现多线程爬取。以下是一个简单的示例:

python

复制代码

import requests

from bs4 import BeautifulSoup

import threading

def fetch(url):

response = requests.get(url)

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

print(soup.title.text)

urls = ['https://www.example.com', 'https://www.example2.com']

threads = []

for url in urls:

t = threading.Thread(target=fetch, args=(url,))

threads.append(t)

t.start()

for t in threads:

t.join()

协程爬取

使用asyncio和aiohttp库可以实现协程爬取。以下是一个简单的示例:

python

复制代码

import asyncio

import aiohttp

from bs4 import BeautifulSoup

async def fetch(session, url):

async with session.get(url) as response:

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

print(soup.title.text)

return soup

async def main():

urls = ['https://www.example.com', 'https://www.example2.com']

async with aiohttp.ClientSession() as session:

tasks = []

for url in urls:

task = asyncio.ensure_future(fetch(session, url))

tasks.append(task)

responses = await asyncio.gather(*tasks)

for response in responses:

print(response)

loop = asyncio.get_event_loop()

loop.run_until_complete(main())

异步IO

使用asyncio和aiohttp库可以实现异步IO爬取。但是我不会,有需要可以自己查文档。

反爬策略及应对

User-Agent伪装

User-Agent是服务器识别客户端的一种方式,我们可以通过伪装User-Agent来达到爬虫不被服务器识别的目的。

在Python中,我们可以使用requests库的headers参数进行User-Agent的伪装。以下是一个简单的示例:

python

复制代码

import requests

url = 'http://example.com'

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

response = requests.get(url, headers=headers)

IP代理使用

IP代理是指通过第三方服务器转发请求,从而隐藏自己的真实IP地址,防止被目标网站封锁。在Python中,我们可以使用requests库配合proxies参数来使用IP代理。以下是一个简单的示例:

python

复制代码

import requests

url = 'http://example.com'

proxies = {

'http': 'http://10.10.1.10:3128',

'https': 'http://10.10.1.10:1080',

}

response = requests.get(url, proxies=proxies)

验证码处理

验证码是网站为了阻止机器人自动访问而设置的一道障碍。在Python中,我们可以使用pytesseract库和PIL库来处理验证码。以下是一个简单的示例:

首先,我们需要安装这两个库:

bash

复制代码

pip install pytesseract pillow

然后,我们可以使用以下代码来处理验证码:

python

复制代码

from PIL import Image

import pytesseract

def get_captcha_text(image_path):

image = Image.open(image_path)

captcha_text = pytesseract.image_to_string(image)

return captcha_text

针对目前一些稀奇古怪的验证码,常见的方法是有针对性地训练机器学习模型解决。

5 小结与其他

1. 基础库介绍

Python 提供了一些用于网络爬虫的库,如 requests、BeautifulSoup、Scrapy 等。这些库可以帮助我们方便地获取网页内容,解析 HTML,存储数据等。

2. 模拟登录

在爬取需要登录的网站时,我们需要使用到模拟登录的技术。这通常涉及到处理 cookies,或者使用 Selenium 进行自动化操作。

2.1 使用 requests 处理 cookies

python

复制代码

import requests

# 登录网站并获取 cookies

s = requests.Session()

login_data = {'username': 'your_username', 'password': 'your_password'}

r = s.post('http://www.example.com/login', data=login_data)

# 使用 cookies 访问受保护的页面

r = s.get('http://www.example.com/protected')

2.2 使用 Selenium 进行模拟登录

Selenium 是一个强大的网页自动化测试工具,可以模拟用户的各种操作,包括点击按钮、填写表单等。

python

复制代码

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

driver.get("http://www.example.com/login")

elem = driver.find_element_by_name("username")

elem.clear()

elem.send_keys("your_username")

elem = driver.find_element_by_name("password")

elem.clear()

elem.send_keys("your_password")

elem.send_keys(Keys.RETURN)

3. 动态网页爬取(Selenium)

有些网站的内容是通过 JavaScript 动态加载的,这时候我们就需要使用到 Selenium 这样的工具来模拟浏览器行为,获取动态加载的内容。

4. 分布式爬虫

当需要爬取大量数据时,我们可以使用分布式爬虫来提高爬取效率。常用的分布式爬虫框架有 Scrapy-Redis、PySpider 等。

相关推荐

win7声音设置教程
365彩票软件app下载

win7声音设置教程

⏱️ 10-16 👁️ 4943
金主是什么意思(金主是什么意思啊)
365bet手机娱乐

金主是什么意思(金主是什么意思啊)

⏱️ 07-06 👁️ 651
正在阅读:480p720p1080p清晰度有什么区别 480p720p1080p清晰度区别介绍【详解】480p720p1080p清晰度有什么区别 480p720p1080p清晰度区别介绍【详解】
虚需要调理多久,吃什么中药
365彩票软件app下载

虚需要调理多久,吃什么中药

⏱️ 10-23 👁️ 3742
【艺术\创意\设计】抽象化的设计概念-坟墓
Bet体育365提款流水

【艺术\创意\设计】抽象化的设计概念-坟墓

⏱️ 01-12 👁️ 7126
最新(2023)年申请苹果企业账号的流程
365bet手机娱乐

最新(2023)年申请苹果企业账号的流程

⏱️ 10-17 👁️ 8980