python 爬虫总结

一,爬虫是什么
爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息。

二,爬虫的基本构架
爬虫分为五个基本构架:

  • 调度器:相当于一台电脑的CPU,主要负责调度URL管理器、下载器、解析器之间的协调工作。
  • URL管理器:包括待爬取的URL地址和已爬取的URL地址,防止重复抓取URL和循环抓取URL,实现URL管理器主要用三种方式,通过内存、数据库、缓存数据库来实现。
  • 网页下载器:通过传入一个URL地址来下载网页,将网页转换成一个字符串,网页下载器有urllib2(Python官方基础模块)包括需要登录、代理、和cookie,requests(第三方包)
  • 网页解析器:将一个网页字符串进行解析,可以按照我们的要求来提取出我们有用的信息,也可以根据DOM树的解析方式来解析。网页解析器有正则表达式(直观,将网页转成字符串通过模糊匹配的方式来提取有价值的信息,当文档比较复杂的时候,该方法提取数据的时候就会非常的困难)、html.parser(Python自带的)、beautifulsoup(第三方插件,可以使用Python自带的html.parser进行解析,也可以使用lxml进行解析,相对于其他几种来说要强大一些)、lxml(第三方插件,可以解析 xml 和 HTML),html.parser 和 beautifulsoup 以及 lxml 都是以 DOM 树的方式进行解析的。
  • 数据存储器:用于将HTML解析器解析出来的数据通过文件或者数据库形式储存起来

三,浏览网页时经历的过程
浏览器 (请求request)-> 输入URL地址(http://www.baidu.com/index.html file:///mnt ftp://172.25.254.250/pub
-> http协议确定, www.baidu.com访问的域名确定 -> DNS服务器解析到IP地址
-> 确定要访问的网页内容 -> 将获取到的页面内容返回给浏览器(响应过程)

四,爬取网页

1).基本方法

from urllib import request
from urllib.error import URLError
try:
    response = request.urlopen('http://www.baidu.com')
    content = response.read().decode('utf-8')
    print(content)
except URLError as e:
    print("访问超时",e.reason)

在这里插入图片描述
2).使用Reauest对象(可以添加其他的头部信息)

from  urllib import  request
from urllib.error import URLError


url = 'http://www.cbrc.gov.cn/chinese/jrjg/index.html'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0'}
try:
    # 实例化request对象, 可以自定义请求的头部信息;
    req = request.Request(url, headers=headers)
    # urlopen不仅可以传递url地址, 也可以传递request对象;
    content = request.urlopen(req).read().decode('utf-8')
    print(content)
except URLError as e:
    print(e.reason)
else:
    print("success")

在这里插入图片描述

** 后续添加头部信息
from  urllib import  request
from urllib.error import URLError
url = 'http://www.cbrc.gov.cn/chinese/jrjg/index.html'
user_agent = ':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0''
try:
    # 实例化request对象, 可以自定义请求的头部信息;
    req = request.Request(url)
    req.add_header('User-Agent',user_agent)
    # urlopen不仅可以传递url地址, 也可以传递request对象;
    content = request.urlopen(req).read().decode('utf-8')
    print(content)
except URLError as e:
    print(e.reason)
else:
    print("success")

在这里插入图片描述

反爬虫策略

1) 模拟浏览器

1.Android
    Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19
    Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
    Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
    2.Firefox

    Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
    Mozilla/5.0 (Android; Mobile; rv:14.0) Gecko/14.0 Firefox/14.0
    3.Google Chrome

    Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36
    Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19
    4.iOS
    Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3
  1. IP代理
当抓取网站时, 程序的运行速度很快, 如果通过爬虫去访问, 一个固定的ip访问频率很高,
网站如果做反爬虫策略, 那么就会封掉ip;

如何解决?
    - 设置延迟;time.sleep(random.randint(1,5))
    - 使用IP代理, 让其他IP代替你的IP访问;
如何获取代理IP?
    http://www.xicidaili.com/
如何实现步骤?
    1). 调用urllib.request.ProxyHandler(proxies=None);传入一个代理,这个代理是一个字典,字典的key依赖于代理服务器能够接收的类型,一般是http或者是https,值是 'ip:port'(免费代理服务器可以选择http://www.xicidaili.com/wt/)
    2). 使用上一步创建的Handler,以及request.build_opener创建一个opener
    3).安装不同的opener对象作为urlopen()使用的全局opener。
    4). 代理IP的选择
from  urllib import  request
from urllib.error import URLError
# url = 'https://www.whatismyip.com/'
url = 'https://httpbin.org/get'
proxy = {'https':'171.221.239.11:808', 'http':'218.14.115.211:3128'}
user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0'
# 1).调用urllib.request.ProxyHandler(proxies=None);传入一个代理,这个代理是一个字典,字典的key依赖于代理服务器能够接收的类型,一般是http或者是https,值是 'ip:port'(免费代理服务器可以选择http://www.xicidaili.com/wt/)
handler = request.ProxyHandler(proxy)
# 2).调用Opener - -- 使用上一步创建的Handler,以及request.build_opener创建一个opener
opener = request.build_opener(handler)
# 伪装浏览器
opener.addheaders = [('User-Agent',user_agent)]
# 3).安装不同的opener对象作为urlopen()使用的全局opener。
request.install_opener(opener)
# 4).代理IP的选择
response = request.urlopen(url)
content  = response.read().decode('utf-8')

print(content)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zcx1203/article/details/83064136