版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhdl11/article/details/38865413
在Python3中,整合了原来的urllib与urllib2,有一些常用操作也是有变化,在学习的过程中,找到的很多例子都是在python2上运行的,很忧桑。。。
下面是写的一些python3中常用操作的代码:
error:
# ----------------------------------
__author__ = 'zWX231671'
__description__ = ''
# ----------------------------------
import urllib.request
url1 = 'http://www.baidu.com/'
url2 = 'http://127.0.0.1:8080/test/index.jsp'
req = urllib.request.Request(url2)
try:
response = urllib.request.urlopen(req)
except urllib.error.HTTPError as e:
print(e.code)
print('the server couldn\'t fulfill the request')
except urllib.error.URLError as e:
print(e.code)
print('failed to reach a server')
else:
print('good job!')
print(response.read().decode('utf-8'))
认证:
# ----------------------------------
__author__ = 'zWX231671'
__description__ = ''
# ----------------------------------
import urllib.request
top_url = 'http://localhost:8080/test/index.jsp'
# 创建密码管理器
passwordmanager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
# 添加用户名及密码
passwordmanager.add_password(None, top_url, 'abc', '123')
handler = urllib.request.HTTPBasicAuthHandler(passwordmanager)
# create opener
opener = urllib.request.build_opener(handler)
# use the opener to fetch a URL
low_url = 'http://localhost:8080/test/index.jsp'
x = opener.open(low_url)
print(x.read())
# install the opener, all calls will use our opener to open
urllib.request.install_opener(opener)
response = urllib.request.urlopen(low_url)
html = response.read()
print(html.decode('utf-8'))
代理:
# ----------------------------------
__author__ = 'zWX231671'
__description__ = ''
# ----------------------------------
import urllib.request
url = 'http://localhost:8080/test/index.jsp'
proxy = urllib.request.ProxyHandler({'sock5': 'localhost:8090'})
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)
html = response.read()
print(html.decode('utf-8'))
timeout:
# ----------------------------------
__author__ = 'zWX231671'
__description__ = ''
# ----------------------------------
import socket
import urllib.request
url1 = 'http://localhost:8080/test/index.jsp'
url2 = 'http://www.baidu.com/'
timeout = 2
socket.setdefaulttimeout(timeout)
req = urllib.request.Request(url2)
response = urllib.request.urlopen(req)
html = response.read()
print(html.decode('utf-8'))
request header设置:
# ----------------------------------
__author__ = 'zWX231671'
__description__ = ''
# ----------------------------------
import urllib.parse
import urllib.request
url = 'http://127.0.0.1:8080/test/index.jsp'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {
'name': 'abc',
'password': '123'
}
data = urllib.parse.urlencode(values)
# that params output from urlencode is encoded to bytes before it is sent to urlopen as data
data = data.encode('utf-8')
headers = {'User-Agent': user_agent}
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
html = response.read()
print(html.decode('utf-8'))