• 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
    
    #原文链接:https://blog.csdn.net/aaronjny/article/details/62088640
    
  • 相关阅读:
    下拉菜单的option的value属性值问题
    GDAL1.9.1 IN VS2008 C#中的编译及使用
    多表连接 去重
    【示例代码】HTML+JS 画图板源码分享
    Winet API 支持HTTPP/SOCKS代理
    入门Html
    关于CDC在非控件类中的使用
    The document "ViewController.xib" could not be opened. Could not read archive.
    华为的一道题
    [置顶] WEBSOKET服务器搭建
  • 原文地址:https://www.cnblogs.com/g2thend/p/11892608.html
Copyright © 2020-2023  润新知