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'))

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

标题:

python之request,ProxyHandler的运用_第1张图片

#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())

你可能感兴趣的:(python之request,ProxyHandler的运用)