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()
==================================================================================
相关推荐
本文介绍urllib库在不同版本的Python中的变动,并以Python3.X讲解urllib库的相关用法。 urllib库对照速查表 Python2.X Python3.X urllib urllib.request, urllib.error, urllib.parse urllib2 urllib.request...
Python3中将python2.7的urllib和urllib2两个包合并成了⼀个urllib库,其主要包括⼀下模块: urllib.request 请求模块 urllib.error 异常处理模块 urllib.parse url解析模块 urllib.robotparser robots.txt解析模块 ...
urllib3 最新版本爬虫 whl安装包 python2-3可用 32位 可用
Python urllib urllib2 urllib模块
Python urllib urllib2 urllib模块安装说明
python库,解压后可用。 资源全名:urllib3-1.24.3-py2.py3-none-any.whl
资源分类:Python库 所属语言:Python 资源全名:urllib3-1.25.9.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
在Pytho2.x中使用import urllib——-对应的,在Python3.x中会使用import urllib.request,urllib.error,urllib.parse 在Pytho2.x中使用import urlparse——-对应的,在Python3.x中会使用import urllib
使用Python 3.x版本运行py文件时报错 No module named 'urllib3' 一开始参照网上的安装方法通过pip来安装,未成功pip install urllib3 后面找到了这个文件,分享给大家。 使用方法是下载解压后,cmd里切换到解压...
python urllib3 安装文件包
2021.12.21 urllib和requests.ipynb
python urllib3 requests
刚刚接触爬虫,基础的东西得时时回顾才行,这么全面的帖子无论如何也得...3. urllib.parse url 解析模块 4. urllib.robotparser robots.txt 解析模块 Urllib 库下的几种模块基本使用如下: urllib.request 关于 urlli
python3对urllib和urllib2进行了重构,拆分成了urllib.request,urllib.response, urllib.parse, urllib.error等几个子模块,这样的架构从逻辑和结构上说更加合理。urllib库无需安装,python3自带。python 3.x中将...
所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,...urllib2在python3.x中被改为urllib.request urlopen 我们先来段代码: #-*- coding:utf-8 -*- #01.urllib2_urlopen.py #导入urllib2库 import
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
urllib3 is a python lib 1.线程安全 2.连接池 3.客户端SSL/TLS验证 4.文件分部编码上传 5.协助处理重复请求和HTTP重定位 6.支持压缩编码 7.支持HTTP和SOCKS代理
离线安装包,亲测可用