python3.x 的urllib使用例子
2014-11-27 11:01
323 查看
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)
================================================================================
# get the code of a given URL as html text string
# Python3 uses urllib.request.urlopen()
# get the encoding used first
# tested with Python 3.1 with the Editra IDE
import urllib.request
def extract(text, sub1, sub2):
"""
extract a substring from text between first
occurances of substrings sub1 and sub2
"""
return text.split(sub1, 1)[-1].split(sub2, 1)[0]
fp = urllib.request.urlopen("http://www.python.org")
mybytes = fp.read()
encoding = extract(str(mybytes).lower(), 'charset=', '"')
print('-'*50)
print( "Encoding type = %s" % encoding )
print('-'*50)
if encoding:
# note that Python3 does not read the html code as string
# but as html code bytearray, convert to string with
mystr = mybytes.decode(encoding)
print(mystr)
else:
print("Encoding type not found!")
fp.close()
==================================================================================
参考:http://www.daniweb.com/forums/thread213221.html
===============================================================================
# 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)
================================================================================
# get the code of a given URL as html text string
# Python3 uses urllib.request.urlopen()
# get the encoding used first
# tested with Python 3.1 with the Editra IDE
import urllib.request
def extract(text, sub1, sub2):
"""
extract a substring from text between first
occurances of substrings sub1 and sub2
"""
return text.split(sub1, 1)[-1].split(sub2, 1)[0]
fp = urllib.request.urlopen("http://www.python.org")
mybytes = fp.read()
encoding = extract(str(mybytes).lower(), 'charset=', '"')
print('-'*50)
print( "Encoding type = %s" % encoding )
print('-'*50)
if encoding:
# note that Python3 does not read the html code as string
# but as html code bytearray, convert to string with
mystr = mybytes.decode(encoding)
print(mystr)
else:
print("Encoding type not found!")
fp.close()
==================================================================================
参考:http://www.daniweb.com/forums/thread213221.html
相关文章推荐
- python3.x 的urllib使用例子
- python3.x 的urllib使用例子
- python3.X使用urllib与2.X不同
- Python3.x学习笔记[2.5]灵活使用urllib与re
- Python 3.X 要使用urllib.request 来抓取网络资源。转
- python urllib的使用介紹
- 在python中可以使用urllib方便的实现图片和flash下载
- python urllib使用
- Python 标准库 urllib2 的使用细节
- [转] 最简单的使用UDP通信的Python Socket例子
- python [3.2] urllib的使用
- 使用python urllib2获取网页信息
- Python入门的36个例子 之 35 -> try – 一个使用的例子
- python使用amqp的例子
- python 中使用ftpmirror的例子
- Python 标准库 urllib2 的使用细节
- Python 标准库 urllib2 的使用细节(http://zhuoqiang.me/a/python-urllib2-usage)
- python urllib2使用
- 【用Python写爬虫】获取html的方法【一】:使用urllib
- Python 标准库 urllib2 的使用细节