02-urllib-请求头添加与IP代理与handler

第2讲

整体课程知识点查看 :https://blog.csdn.net/j1451284189/article/details/128713764

知识点
urllib请求头添加
urllibIP代理使用
urllib handler相关

一、get传参

1、汉字报错:解释器ASCII码没有汉字,url汉字需转码

urllib.parse.quote(safe = ‘string.printtable’)

2、字典传参

urllib.parse.urlencode(params) #参数转换为url内的形式


def day2_get_params():
    #字典传参相关
    url = 'http://www.baidu.com/s?wd='
    params  = {
        'wd': '中文',
        'key': 'zhang',
        'value': 'san'
    }
    #参数转换为url内的形式
    str_params = urllib.parse.urlencode(params)
    final_url = url+str_params
    print(final_url)
    #将带有中文的URL转译成可识别的
    end_url = urllib.parse.quote(final_url, safe=string.printable)
    response = urllib.request.urlopen(end_url)
    data = response.read().decode('utf-8')
    print(end_url)
    print(data)

二、post请求

urllib.request.urlopen(url,data=‘服务器接收的数据’)

三、请求头信息添加

模拟真实浏览器发生请求 (百度批量搜索)

request = urllib.request.Request(url)  #创建请求对象

request = urllib.request.Request(url,headers=headers)#创建请求对象,并添加请求头
request.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0') #动态添加请求头信息

request_headers = request.headers #获取请求头数据
request_headers = request.headers #获取请求头数据内参数信息
full_url = request.get_full_url()#获取请求的完整的url

request_headers = response.headers #查看响应头信息

def day2_request_header():
    url = 'http://www.baidu.com/'
    #创建请求对象
    request = urllib.request.Request(url)
    #请求网络数据
    response = urllib.request.urlopen(request)
    #获取请求头数据
    print(request.headers)
    #查看响应头信息
    # print(response.headers)
  

def day2_request_header2():
    #添加请求头信息
    url = 'https://www.baidu.com/'
    headers= {
        #浏览器的版本
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0'
    }
    # #创建请求对象,并添加请求头
    # request = urllib.request.Request(url,headers=headers)
    #动态添加请求头信息
    request = urllib.request.Request(url)
    request.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0')
    #请求网络数据
    response = urllib.request.urlopen(request)
    #获取请求头数据
    print(request.headers)
    print(request.get_header('User-agent'))#注意,首字母大写,其余均小写,否则返回None
    #获取完整的url
    final_url = request.get_full_url()
    print(final_url)
    #查看响应头信息
    # print(response.headers)
    
def day2_random_agent():
    #随机获取浏览器,
    url = 'http://www.baidu.com/'
    user_agent_list = [
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1',
        'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
        'Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50',
    ]
    random_user_agent = random.choice(user_agent_list)
    request = urllib.request.Request(url)
    request.add_header('User-Agent', random_user_agent)
    response = urllib.request.urlopen(request)
    print(request.get_header('User-agent'))

四、IP代理

1、免费的IP:时效差,错误性高

2、付费IP:贵花钱,失效

3、IP分类

透明:对方知道我们真实的IP

匿名:对方不知道我们真实IP,知道你使用了代理

高匿:对方不知道我们真实IP,也不知道使用了代理

五、handler原理与代理

系统的urlopen不支持代理的添加,需自己创建handler

创建代理处理器ProxyHandler

使用ProxyHandler创建opener

使用opener调用open方法请求数据

#总结
handler = urllib.request.HTTPHandler()#创建自己的处理器
opener = urllib.request.build_opener(handler)#创建自己的openner    
response = opener.open(url)#用自己创建的openner调用open请求数据
#代理的写法 免费与付费的不同
proxy_handler = urllib.request.ProxyHandler(proxy)#创建代理处理器


def day2_handler_opener():
    """自定义urlopen类似功能"""
    #系统的urlopen并没有添加代理功能,需自定义
    #安全套接层 SSL第三方的CA数字证书
    #http 80端口和https 443端口
    # urllib.request.urlopen() 原理 handler处理器  自己的opener请求数据
    url= 'https://blog.csdn.net/jjxp2011/article/details/124546976'
    #创建自己的处理器
    handler = urllib.request.HTTPHandler()
    #创建自己的openner
    opener = urllib.request.build_opener(handler)
    #用自己创建的openner调用open请求数据
    response = opener.open(url)
    data = response.read()
    print(response)
    print(data)
    
def day2_proxy_handler():
    """创建使用代理IP的opener"""
    url = 'https://blog.csdn.net/jjxp2011/article/details/124546976'
    #添加代理
    proxy={
        #免费的写法
        # 'http':'http://120.77.249.46:8080'
        'http': '120.77.249.46:8080'
        #付费代理的写法
        #'http':'xiaoming':123@115
    }
    #代理处理器
    proxy_handler = urllib.request.ProxyHandler(proxy)
    #创建自己opener
    opener = urllib.request.build_opener(proxy_handler)
    #用代理IP请求
    data = opener.open(url).read()
    print(data)
 

def day2_random_proxy():
    """爬虫多代理IP"""
    url = 'https://blog.csdn.net/jjxp2011/article/details/124546976'
    proxy_list = [
        { 'http': '120.77.249.46:8080'},
        { 'http': '18.191.216.4:80'},
        { 'http': '103.123.234.106:8080'},
        { 'http': '41.79.37.74:8585'},
        { 'http': '117.4.115.169:8080'},
        { 'http': '8.242.207.202:8080'},
        { 'http': '103.48.183.113:4145 '},
    ]
    for proxy in proxy_list:
        print(proxy)
        #利用proxy创建handler
        proxy_handler = urllib.request.ProxyHandler(proxy)
        opener = urllib.request.build_opener(proxy_handler)
        try:
            response = opener.open(url,timeout=1)
        except Exception as err:
            print(str(err))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值