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
- GoodZhang在学Python(十四)--Python3中urllib库常用操作
- GoodZhang在学Python(一)--初探Python
- GoodZhang在学Python(六)--字符串格式化
- GoodZhang在学Python(八)--基本数据结构
- GoodZhang在学Python(十三)--Python3数据get与post提交
- GoodZhang在学Python(二)--Python环境安装配置&HelloWorld
- GoodZhang在学Python(四)--Python中的函数
- GoodZhang在学Python(七)--代码重用之模块
- GoodZhang在学Python(九)--序列和参考
- GoodZhang在学Python(十)--类和对象
- GoodZhang在学Python(十一)--下一步应该学啥
- GoodZhang在学Python(十二)--简单客户端、服务端socket通信
- GoodZhang在学Python(五)--Python中的文档字符串(docStrings)
- GoodZhang在学Python(三)--控制流if while for break continue
- python学习笔记(十四)标准库urllib
- python3中urllib库的使用
- python3 标准库 urllib
- Python3 中urllib的使用
- Linux新手大礼包:学习Linux你必须知道的那些事儿
- ARM与X86架构对比区别
- STL array的crend方法(8)
- 程序员小故事-战术上的勤奋和战略上的懒惰
- CF 461C - Appleman and a Sheet of Paper (树状数组)
- GoodZhang在学Python(十四)--Python3中urllib库常用操作
- Struts2中的异常处理
- ORA-01791: not a SELECTed expression after upgrade to 11.2.0.4 (文档 ID 1600974.1)
- 二维码的生成细节和原理
- Winform动态设置TableLayoutPanel等宽高
- ie6不识position:fixed,固定定位的其它实现
- Codeforces Round #263 (Div. 2) A.Appleman and Easy Task(坑)
- kill命令
- Hibernate简单明了的入门博客