python之request,ProxyHandler的运用

标题:在这里插入图片描述


#encoding = utf -8

from urllib import request,parse

url = 'https://www.lagou.com/landing-page/pc/communal.html?utm_source=m_cf_cpt_sogou_pc'

#resp = request.urlopen(url)
#print(resp.read())
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400',
    'Referer':'https://www.sogou.com/sgo?query=%E6%8B%89%E5%8B%BE%E7%BD%91&hdq=sogou-site-7d3010c11d08cf99&lkx=0&lxod=0_512_0_-1_1&lxea=5-2-0-9.6.0.3568-3-CN1407-128-1-1-3FB5F6D8046FB45C5A4995090976CDF0-3&lxoq=lag&fr=qqbrowser&ie=utf8'
}
data = {
    'first':'true',
    'pn':'1',
    'kd':'python'
}
req = request.Request(url,headers=headers,data=parse.urlencode(data).encode('utf-8'),method='POST')
resp = request.urlopen(req)
print(resp.read().decode('utf-8'))

注: , . 的区别,字母大小写的区别

标题:

在这里插入图片描述

#encoding = utf -8

from urllib import request
# 没有使用代理的
#url = 'http://httpbin.org/ip'
#resp = request.urlopen(url)
#print(resp.read)

# 使用代理的
url = 'http://httpbin.org/ip'
#  1.使用ProxyHandle,传入代理构建一个handler
handler = request.ProxyHandler({"Http":'125.108.89.56:9000'})
# 2.使用上面创建的的 handler 构建一个 opener
opener = request.build_opener(handler)
# 3.使用opener去发送一个请求
resp = opener.open(url)
print(resp.read())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值