zoukankan      html  css  js  c++  java
  • python爬虫添加请求头代码实例

    这篇文章主要介绍了python爬虫添加请求头代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    request  

    import requests
    headers = {
      # 'Accept': 'application/json, text/javascript, */*; q=0.01',
      # 'Accept': '*/*',
      # 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7',
      # 'Cache-Control': 'no-cache',
      # 'accept-encoding': 'gzip, deflate, br',
      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36',
      'Referer': 'https://www.google.com/'
    }
    
    resp = requests.get('http://httpbin.org/get', headers=headers)
    print(resp.content)

    urllib

    import urllib, urllib2
    def get_page_source(url):
      headers = {'Accept': '*/*',
            'Accept-Language': 'en-US,en;q=0.8',
            'Cache-Control': 'max-age=0',
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
            'Connection': 'keep-alive',
            'Referer': 'http://www.baidu.com/'
            }
      req = urllib2.Request(url, None, headers)
      response = urllib2.urlopen(req)
      page_source = response.read()
      return page_source

    phantomjs请求页面

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    def get_headers_driver():
      desire = DesiredCapabilities.PHANTOMJS.copy()
      headers = {'Accept': '*/*',
            'Accept-Language': 'en-US,en;q=0.8',
            'Cache-Control': 'max-age=0',
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
            'Connection': 'keep-alive',
            'Referer': 'http://www.baidu.com/'
            }
      for key, value in headers.iteritems():
        desire['phantomjs.page.customHeaders.{}'.format(key)] = value
      driver = webdriver.PhantomJS(desired_capabilities=desire, service_args=['--load-images=yes'])#将yes改成no可以让浏览器不加载图片
      return driver

    主要介绍了python爬虫添加请求头代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持python博客。

    http://blog.sina.com.cn/s/blog_184e9f38b0102z269.html
    https://tieba.baidu.com/p/7168142321

    1、为什么要设置headers? 

    在请求网页爬取的时候,输出的text信息中会出现抱歉,无法访问等字眼,这就是禁止爬取,需要通过反爬机制去解决这个问题。

    headers是解决requests请求反爬的方法之一,相当于我们进去这个网页的服务器本身,假装自己本身在爬取数据。

    对反爬虫网页,可以设置一些headers信息,模拟成浏览器取访问网站 。

    http://www.jzb.com/bbs/thread-7868082-1-1.html

    2、 headers在哪里找? 

    谷歌或者火狐浏览器,在网页面上点击:右键–>检查–>剩余按照图中显示操作,需要按Fn+F5刷新出网页来 

    有的浏览器是点击:右键->查看元素,刷新

    以上就是python爬虫请求头设置代码的详细内容,更多关于python爬虫请求头如何设置的资料请关注脚本之家其它相关文章!

  • 相关阅读:
    ural(Timus) 1019 Line Painting
    ACMICPC Live Archive 2031 Dance Dance Revolution
    poj 3321 Apple Tree
    其他OJ 树型DP 选课
    poj 3548 Restoring the digits
    ACMICPC Live Archive 3031 Cable TV Network
    递归循环获取指定节点下面的所有子节点
    手动触发asp.net页面验证控件事件
    子级Repeater获取父级Repeater绑定项的值
    没有列名的数据绑定
  • 原文地址:https://www.cnblogs.com/yuqiao668/p/14191298.html
Copyright © 2011-2022 走看看