Python发起URL请求

urllib

# 发起请求 
    req = urllib.request.urlopen(url,data=data,timeout=0.1)

    req = urllib.request.Request(url,data=data,headers = headers,method = 'POST')
    rep = request.urlopen(req)
# 响应
    rep = req.read().decode('utf-8')
# 响应状态
    rep.status
# 响应头  
    rep.getheaders()
# 下载文件
    urllib.urlretrieve(url,filename=None,reporthook=None,data=None)
# 异常处理 
    urllib.error.URLError()
# url解析 
    urllib.parse.urlenocde()
# robots.txt解析
    urllib.robotparse()
# 代理
    proxy_handler = urllib.request.ProxyHandler({'http':'http://127.0.0.1:1080'})
    opener = urllib.request.build_opener(proxy_handler)
    rep = opener.open(url)

urllib2

# 发起请求
    urllib2.urlopen()

requests

# 发起请求
    r = requests.get(url)
    r = requests.post(url)
    r = requests.put(url)
    r = requests.delete(url)
    r = requests.head(url)
    r = requests.options(url)

# 为URL传递参数
    payload = {'key1':'value1','key2':'value2'}
    r = requests.get(url,params = payload)
# 查看URL 
    print(r.url)
# 响应内容
    r = requests.get(url)
    r.text
    r.encoding 'utf-8'
    r.encoding = 'ISO-8859-1'
# 二进制响应内容
    r = requests.get('url')
    r.content
# 请求头
    url = 'http://www.baidu.com'
    headers = {'content-type':'application/json'}
    r = requests.get(url, headers = headers)
# post数据
    payload = {'key1':'value1','key2':'value2'}
    r = requests.post(url,data = payload)
# 响应状态码
    r = requests.get(url)
    r.status_code
# 响应头
    r.headers
Cookies
    r.cookies
    r.cookies['uid']
# 超时
    r = requests.get(url,timeout = 2)
# 文件上传
    files = {'file':open('1.jpg','rb')}
    reponse = request.post(url,files = files)
# 会话维持
    s = request.Session()
    s.get(url)
    response = s.get(url)
# ssl证书验证
    response = requests.get(url,verify=False) #关闭验证
    response = requests.get(url,cert=('/path/server.crt','/path/key')) #手动导入证书
# 设置代理
    http/https
    	proxies = {'http':'http://127.0.0.1:1080','https','https://127.0.0.1:1080'}
    	response = requests.get(url,proxies = proxies)
    socks
    	pip3 install 'requests[socks]'
    	proxies = {'http':'socks5://127.0.0.1:1080','https','socks5://127.0.0.1:1080'}
    	response = requests.get(url,proxies = proxies)
# 认证
    response = requests.get(url,auth={'userName','password'})

文章作者: 子杰
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 子杰 ! !
评论
  目录