美文网首页python学习🕸
Python学习九十天:突破反爬虫策略

Python学习九十天:突破反爬虫策略

作者: 暖A暖 | 来源:发表于2019-05-22 09:45 被阅读16次

1.什么是爬虫和反爬虫

  • 爬虫是使用任何技术手段批量获取网站信息的一种方式,反爬虫是使用任何技术手段阻止别人批量获取自己网站信息的一种方式;

2.User-Agent介绍

  • User Agent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,User Agent也简称UA。它是一个特殊字符串头,是一种向访问网站提供你所使用的浏览器类型及版本、操作系统及版本、浏览器内核、等信息的标识;

  • User-Agentheaders中的一个属性,表示当前访问服务器的身份信息,如果同一个身份过于频繁的访问服务器会被识别为机器身份,遭到反爬的打击,所以需要频繁的更改User-Agent信息;

  • User-Agent字段解析:浏览器标识 (操作系统标识; 加密等级标识; 浏览器语言) 渲染引擎标识 版本信息;

3.使用不同的User-Agent来规避反爬策略

  • 想要随机更改User-Agent,首先我们可以在蜘蛛文件的Spider类中添加一个header请求头,很多网站只需要userAgent信息就可以通过,但是有的网站还需要验证一些其他的信息,所以我们可以在请求头中添加一些需要用到的字段,比如:

  • Accept:客户端支持的数据类型,用逗号隔开,是有顺序的,分号前面是主类型,分号后是子类型;

  • Accept-Encoding:指定浏览器可以支持的web服务器返回内容压缩编码类型;

  • Accept-Language:浏览器可接受的自然语言的类型;

  • Connection:设置HTTP连接的持久化,通常都是Keep-Alive;

  • host:服务器的域名或IP地址,如果不是通用端口,还包含该端口号;

  • Referer:指当前请求的URL是在什么地址引用的;

headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding':  'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection':  'keep-alive',
        'host': 'dribbble.com/stories',
        'Referer': 'https://dribbble.com/',
    }
  • 添加完请求头需要的字段,我们可以在settings.py 文件中手动创建一个User-Agent列表
user_agent_list = [
    "Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16",
    "Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14",
    "Mozilla/5.0 (Windows NT 6.0; rv:2.0) Gecko/20100101 Firefox/4.0 Opera 12.14",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0) Opera 12.14",
    "Opera/12.80 (Windows NT 5.1; U; en) Presto/2.10.289 Version/12.02",
    "Opera/9.80 (Windows NT 6.1; U; es-ES) Presto/2.9.181 Version/12.00",
    "Opera/9.80 (Windows NT 5.1; U; zh-sg) Presto/2.9.181 Version/12.00",
    "Opera/12.0(Windows NT 5.2;U;en)Presto/22.9.168 Version/12.00",
    "Opera/12.0(Windows NT 5.1;U;en)Presto/22.9.168 Version/12.00",
    "Mozilla/5.0 (Windows NT 5.1) Gecko/20100101 Firefox/14.0 Opera/12.0",
    "Opera/9.80 (Windows NT 6.1; WOW64; U; pt) Presto/2.10.229 Version/11.62",
    "Opera/9.80 (Windows NT 6.0; U; pl) Presto/2.10.229 Version/11.62",
    "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
    "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; de) Presto/2.9.168 Version/11.52",
    "Opera/9.80 (Windows NT 5.1; U; en) Presto/2.9.168 Version/11.51",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; de) Opera 11.51",
    "Opera/9.80 (X11; Linux x86_64; U; fr) Presto/2.9.168 Version/11.50",
    "Opera/9.80 (X11; Linux i686; U; hu) Presto/2.9.168 Version/11.50",
    "Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11",
    "Opera/9.80 (X11; Linux i686; U; es-ES) Presto/2.8.131 Version/11.11",
    "Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/5.0 Opera 11.11",
    "Opera/9.80 (X11; Linux x86_64; U; bg) Presto/2.8.131 Version/11.10",
    "Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10",
    "Opera/9.80 (Windows NT 5.1; U; zh-tw) Presto/2.8.131 Version/11.10",
    "Opera/9.80 (Windows NT 6.1; Opera Tablet/15165; U; en) Presto/2.8.149 Version/11.1",
    "Opera/9.80 (X11; Linux x86_64; U; Ubuntu/10.10 (maverick); pl) Presto/2.7.62 Version/11.01",
]
  • 然后在项目中创建一个utils.py文件,在这个文件中自定义一个随机函数
import random
def get_randam_int(lst):
    return random.randint(0, len(lst)-1)
  • 最后修改蜘蛛文件中的parse()方法,导入相关模块,调用自定义的随机函数生成随机User-gent添加到headers请求头中;
    def parse(self, response):
        headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'host': 'dribbble.com',
            'Referer': 'https://dribbble.com/',
        }
        random_index = get_randam_int(user_agent_list)
        random_agent = user_agent_list[random_index]
        headers['User-Agent'] = random_agent
        a_nodes = response.css('header div.teaser a')
        for a_node in a_nodes:
            # print(a_node)
            a_url = a_node.css('::attr(href)').extract()[0]
            a_image_url = a_node.css('img::attr(src)').extract()[0]
            yield Request(headers=headers,url=parse.urljoin(response.url, a_url), callback=self.parse_analyse, meta={'a_image_url': a_image_url})

4.也可以在中间件中设置User Agent

5.调试工具

  • 打开你需要爬虫的网页;

  • 按键盘的F12或手动去浏览器右上角的“更多工具”选项选择开发者工具;

  • 按键盘的F5刷新网页;

  • 选择Network中的Doc;

  • 点击Headers,就可以在最末尾查看Request Headers的User-Agent字段,也可以复制使用User-Agent字段;

参考:https://www.9xkd.com/user/plan-view.html?id=1782598054

相关文章

  • 抖音爬虫教程,python爬虫采集反爬策略

    抖音爬虫教程,python爬虫采集反爬策略一、爬虫与反爬简介爬虫就是我们利用某种程序代替人工批量读取、获取网站上的...

  • Python学习九十天:突破反爬虫策略

    1.什么是爬虫和反爬虫 爬虫是使用任何技术手段批量获取网站信息的一种方式,反爬虫是使用任何技术手段阻止别人批量获取...

  • 反爬虫到底是怎么一回事?

    爬虫与反爬虫永远是相生相克的:当爬虫知道了反爬策略就可以制定反-反爬策略,同样地,网站知道了爬虫的反-反爬策略就可...

  • Python构建代理池

    用 Python 爬取网站内容的时候,容易受到反爬虫机制的限制,而突破反爬虫机制的一个重要措施就是使用IP代理。我...

  • 常见的反爬虫和应对方法

    通过Headers 反爬虫: 从用户请求的Headers 反爬虫是最常见的反爬虫策略。很多网站都会对Headers...

  • 爬虫入门基础

    Day01 一、爬虫介绍 什么是爬虫 Python爬虫的优势 Python爬虫需要掌握什么 爬虫与反爬虫与反反爬虫...

  • Scrapy爬虫——突破反爬虫最全策略解析

    有条件的请支持慕课实战正版课程,本blog仅仅是归纳总结,自用。 一、爬虫与反爬虫基本概念 误伤:由于学校、网吧等...

  • 01-认识爬虫

    一、爬虫介绍 什么是爬虫 Python爬虫的优势 Python爬虫需要掌握什么 爬虫与反爬虫与反反爬虫三角之争 网...

  • 反爬虫策略

    最近在做爬虫相关的作业, 发现一些网站有反爬虫策略。 反爬虫策略说白了就是要检测你是不是机器人。 那问题来了, 如...

  • Python代理IP爬虫的简单使用

    前言 Python爬虫要经历爬虫、爬虫被限制、爬虫反限制的过程。当然后续还要网页爬虫限制优化,爬虫再反限制的一系列...

网友评论

    本文标题:Python学习九十天:突破反爬虫策略

    本文链接:https://www.haomeiwen.com/subject/nlxyzqtx.html