python3-urllib.request操作

请求网页

import urllib.request
f = urllib.request.urlopen('http:www.bing.com')
print(f.read().decode('utf-8'))

#获取状态码
f.getcode()
#获取url
f.geturl()
#获取meta-infomation,像headers
print(f.info())

发送数据请求

import urllib.request
 req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
                        data=b'This data is passed to stdin of the CGI')
f = urllib.request.urlopen(req)
print(f.read().decode('utf-8'))

put请求

import urllib.request
DATA=b'some data'
req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
f = urllib.request.urlopen(req)
print(f.status)
print(f.reason)

基本http验证,登录请求

import urllib.request
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='PDQ Application',
                          uri='https://mahler:8092/site-updates.py',
                          user='klem',
                          passwd='kadidd!ehopper')
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)
urllib.request.urlopen('http://www.example.com/login.html')

支持代理方式验证请求

proxy_handler = urllib.request.ProxyHandler({'http': 'http://www.example.com:3128/'})
proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')

opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
# This time, rather than install the OpenerDirector, we use it directly:
opener.open('http://www.example.com/login.html')

添加headers

import urllib.request
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
opener.open('http://www.example.com/')
#或
import urllib.request
req = urllib.request.Request('http://www.example.com/')
req.add_header('Referer', 'http://www.python.org/')
# Customize the default User-Agent header value:
req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)')
r = urllib.request.urlopen(req)

带参数的post

import urllib.request
import urllib.parse
data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
data = data.encode('ascii')
with urllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f:
     print(f.read().decode('utf-8'))

带参数的get

import urllib.request
import urllib.parse
params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
url = "http://www.musi-cal.com/cgi-bin/query?%s" % params
with urllib.request.urlopen(url) as f:
     print(f.read().decode('utf-8'))

指定代理请求

import urllib.request
proxies = {'http': 'http://proxy.example.com:8080/'}
opener = urllib.request.FancyURLopener(proxies)
f = opener.open("http://www.python.org")
f.read().decode('utf-8')

无代理请求

import urllib.request
opener = urllib.request.FancyURLopener({})
with opener.open("http://www.python.org/") as f:
     f.read().decode('utf-8')

你可能感兴趣的:(python3-urllib.request操作)