GoodZhang在学Python(十四)--Python3中urllib库常用操作

来源:互联网 发布:加好友软件qq2016 编辑:程序博客网 时间:2024/05/21 08:46

在Python3中,整合了原来的urllib与urllib2,有一些常用操作也是有变化,在学习的过程中,找到的很多例子都是在python2上运行的,很忧桑。。。

下面是写的一些python3中常用操作的代码:

error:

# ----------------------------------__author__ = 'zWX231671'__description__ = ''# ----------------------------------import urllib.requesturl1 = '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.requesttop_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 openeropener = urllib.request.build_opener(handler)# use the opener to fetch a URLlow_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 openurllib.request.install_opener(opener)response = urllib.request.urlopen(low_url)html = response.read()print(html.decode('utf-8'))


代理:

# ----------------------------------__author__ = 'zWX231671'__description__ = ''# ----------------------------------import urllib.requesturl = '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 socketimport urllib.requesturl1 = 'http://localhost:8080/test/index.jsp'url2 = 'http://www.baidu.com/'timeout = 2socket.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.parseimport urllib.requesturl = '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 datadata = 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'))



0 0
原创粉丝点击