一、简介

  • 为啥要使用 Handler?
    1、urllib.request.urlopen(url) 不能定制请求头。
    2、urllib.request.Request(url, data, headers) 可以定制请求头。
    3、Handler 定制更高级的请求头(随着业务逻辑的复杂,请求对象的定制已经满足不了需求,比如:动态 cookie和代理不能使用请求对象的定制)。
  • 要爬取的各种各样的网页,有一部填写需要验证码,有的需要 cookie,还有更多许多高级的功能,会阻碍你爬,而对于 openurl 单纯点理解就是打开网页。openurl 打开一个网址,它可以是一个字符串或者是一个 request 对象。而 build_opener 就是多了 handler,处理问题更专,更个性化。
  • 简单使用,通过 Handler 去完成 urlopen 打开网页的操作。
# 使用 urllib
import urllib.request

# 定义 header
headers = {
  # UA 最基本的防爬识别
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}

# 1、定义一个 https 的 url
url = 'https://www.baidu.com'

# 2、定义一个 Request 对象,urlopen 方法并不能直接带 header。
# 细节:为什么这里需要写 url=url 而有的地方不需要?因为 Request 构造方法传参顺序问题 Request(url, data=None, headers={} ...)
request = urllib.request.Request(url=url, headers=headers)

# 3、获取 Handler 对象
handler = urllib.request.HTTPHandler()

# 4、获取 opener 对象
opener = urllib.request.build_opener(handler)

# 5、调用 open 方法
response = opener.open(request)

# 6、获取内容字符串
content = response.read().decode('utf-8')

# 7 输出
print(content)

二、IP代理

  • 使用同一个 IP 去不断地访问爬取数据,容易被对方服务器封 IP,这个时候就需要使用代理,简单点说,就是使用 N 个别人的 IP 去访问爬取数据。
  • 代理配置步骤:(参考上面 简单使用 案例步骤)
    1、创建 Request 对象
    2、创建 ProxyHandler 对象
    3、用 handler 对象创建 opener 对象
    4、使用 opener.open 函数发送请求
  • 使用案例
# 使用 urllib
import urllib.request
# 使用 random
import random

# 定义 header
headers = {
  # UA 最基本的防爬识别
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}

# 方式一:字典形式的IP代理(https://www.kuaidaili.com/free/)
# proxies = { 'http': '112.14.47.6:52024' }

# 方式二:定义一个代理池,然后随机从代理池中获取一个代理
proxies_pool = [
  { 'http':'112.14.47.6:52024' },
  { 'http':'61.164.39.68:53281' }
]
proxies = random.choice(proxies_pool)

# 输出当前代理
print(proxies)

# 1、定义一个 https 的 url
url = 'https://www.baidu.com/s?wd=ip'

# 2、定义一个 Request 对象,urlopen 方法并不能直接带 header。
# 细节:为什么这里需要写 url=url 而有的地方不需要?因为 Request 构造方法传参顺序问题 Request(url, data=None, headers={} ...)
request = urllib.request.Request(url=url, headers=headers)

# 3、获取 Handler 对象
handler = urllib.request.ProxyHandler(proxies=proxies)

# 4、获取 opener 对象
opener = urllib.request.build_opener(handler)

# 5、调用 open 方法
response = opener.open(request)

# 6、获取内容字符串
content = response.read().decode('utf-8')

# 7 保存,可以方便校验IP是否代理成狗
with open('ip.html', 'w', encoding='utf-8') as f:
  f.write(content)