python3.X使用urllib与2.X不同
2014-07-31 20:24
357 查看
2.x版本的python可以直接使用import urllib来进行操作,但是3.x版本的python使用的是import urllib.request来进行操作,下面是简单的例子:
===============================================================================
# get code of given URL as html text string
# Python3 uses urllib.request.urlopen()
# instead of Python2's urllib.urlopen() or urllib2.urlopen()
import urllib.request
fp = urllib.request.urlopen("http://www.python.org")
mybytes = fp.read()
# note that Python3 does not read the html code as string
# but as html code bytearray, convert to string with
mystr = mybytes.decode("utf8")
fp.close()
print(mystr)
=============================================================================
我所遇到的问题:
1.import urllib.request是3.X中的,在2.X中直接用import urllib
2.fp = urllib.request.urlopen("http://www.python.org"),而不是 urllib.urlopen("http://www.python.org")
3.mystr
= mybytes.decode("utf8"),编码需要注意
===============================================================================
# get code of given URL as html text string
# Python3 uses urllib.request.urlopen()
# instead of Python2's urllib.urlopen() or urllib2.urlopen()
import urllib.request
fp = urllib.request.urlopen("http://www.python.org")
mybytes = fp.read()
# note that Python3 does not read the html code as string
# but as html code bytearray, convert to string with
mystr = mybytes.decode("utf8")
fp.close()
print(mystr)
=============================================================================
我所遇到的问题:
1.import urllib.request是3.X中的,在2.X中直接用import urllib
2.fp = urllib.request.urlopen("http://www.python.org"),而不是 urllib.urlopen("http://www.python.org")
3.mystr
= mybytes.decode("utf8"),编码需要注意
相关文章推荐
- python3.x 的urllib使用例子
- python 2.x和3.x中maketrans和translate函数的使用
- 【python学习笔记】python2.x与python3.x版本中zip函数的不同
- Python2.X/Python3.X中urllib库区别讲解
- [Python2.x] 标准库 urllib2 的使用细节
- python笔记之3.x与2.x的使用区别
- 在CentOS7下同时安装、使用Python2.x和Python3.x
- python3.x 和 python2.x关于 urllib的用法
- Python 2.x和3.x不同点
- python(2.x和3.x通用) 中几种容器的使用
- sorted()函数 在Python2.x 和 Python3.x 中有所不同
- python2.x与3.x版本print的不同
- Python3.x学习笔记[2.5]灵活使用urllib与re
- python 2.x和3.x中maketrans和translate函数的使用
- python3.x 和 python2.x关于 urllib的用法
- python3.x 的urllib使用例子
- python3.x使用过程中发现的与2.x的不同
- Python2.X和Python3.X中的urllib区别
- Python 3.X 要使用urllib.request 来抓取网络资源。转
- python3.x 的urllib使用例子