python3 urllib
官方文档
官方文档:https://docs.python.org/3/library/urllib.html
获取页面内容
第一种方式
import urllib.request url = 'https://www.baidu.com/'
r = urllib.request.urlopen(url)
print(r) # <http.client.HTTPResponse object at 0x00000201E6C66CF8>
print(r.read().decode('utf-8'))
另一种方式
import urllib.request url = 'http://www.cnblogs.com/0bug/'
req = urllib.request.Request(url)
res = urllib.request.urlopen(req)
print(res.read().decode('utf-8'))
发送内容
import urllib.request
import urllib.parse url = 'http://httpbin.org/post'
data = bytes(urllib.parse.urlencode({'name': 'lcg'}), encoding='utf-8')
r = urllib.request.urlopen(url, data=data)
print(r.read().decode('utf-8'))
设置超时时间
设置超时时间
import urllib.request url = 'http://www.cnblogs.com/0bug/'
r = urllib.request.urlopen(url, timeout=1) # 设置超时时间
print(r.read().decode('utf8'))
异常处理
import urllib.request
import urllib.error
import socket url = 'http://www.cnblogs.com/0bug/'
try:
r = urllib.request.urlopen(url, timeout=0.01) # 设置超时时间
print(r.read().decode('utf8'))
except urllib.error.URLError as e:
if isinstance(e.reason, socket.timeout):
print('请求超时')
响应码、响应头
import urllib.request url = 'http://www.cnblogs.com/0bug/'
r = urllib.request.urlopen(url)
print(r.status) # 200
print(r.getheaders()) # [(('Content-Type', 'text/html; charset=utf-8'),......]
print(r.getheader('Content-Type')) # text/html; charset=utf-8
构造请求信息
import urllib.request
import urllib.parse url = 'http://www.cnblogs.com/0bug/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36',
'Host': 'www.cnblogs.com'
}
dic = {'name': 'lcg'}
data = bytes(urllib.parse.urlencode(dic), encoding='utf-8')
req = urllib.request.Request(url=url, data=data, headers=headers, method='POST')
res = urllib.request.urlopen(req)
print(res.read().decode('utf-8'))
另一种添加请求头的方式
import urllib.request
import urllib.parse url = 'http://www.cnblogs.com/0bug/'
dic = {'name': 'lcg'}
data = bytes(urllib.parse.urlencode(dic), encoding='utf-8')
req = urllib.request.Request(url=url, data=data, method='POST')
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/53...')
res = urllib.request.urlopen(req)
print(res.read().decode('utf-8'))
代理
import urllib.request url = 'http://www.cnblogs.com/0bug/'
proxy_handler = urllib.request.ProxyHandler({
'http': 'http://122.114.31.177:808',
'https': 'https://124.133.75.183:8118'
})
opener = urllib.request.build_opener(proxy_handler)
r = opener.open(url)
print(r.read().decode('utf-8'))
python3 urllib的更多相关文章
- 爬虫小探-Python3 urllib.request获取页面数据
使用Python3 urllib.request中的Requests()和urlopen()方法获取页面源码,并用re正则进行正则匹配查找需要的数据. #forex.py#coding:utf-8 ' ...
- 【转】python3 urllib.request 网络请求操作
python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...
- Python3 urllib.request库的基本使用
Python3 urllib.request库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 在Python中有很多库可以用来抓取网页,我们先学习urlli ...
- python3 urllib 类
urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google ...
- python3 urllib.request 网络请求操作
python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...
- Python3 urllib.parse 常用函数示例
Python3 urllib.parse 常用函数示例 http://blog.51cto.com/walkerqt/1766670 1.获取url参数. >>> from url ...
- python2 与 python3 urllib的互相对应关系
urllib Python2 name Python3 nameurllib.urlopen() Deprecated. See urllib.request.urlopen() which mirr ...
- python3 urllib.request.urlopen() 地址打开错误
错误内容:UnicodeEncodeError: 'ascii' codec can't encode characters in position 28-29: ordinal not in ran ...
- python3 urllib及requests基本使用
在python中,urllib是请求url连接的标准库,在python2中,分别有urllib和urllib,在python3中,整合成了一个,称谓urllib 1.urllib.request re ...
- (转)python3 urllib.request.urlopen() 错误UnicodeEncodeError: 'ascii' codec can't encode characters
代码内容: url = 'https://movie.douban.com/j/search_subjects?type=movie'+ str(tag) + '&sort=recommend ...
随机推荐
- ReactJS环境搭建
1.ReactJs 需要依赖nodejs环境,如果没有安装nodejs的话,需要安装.下载地址:https://nodejs.org/en/download/ 下载下来之后,安装windows版本的m ...
- en_e outtest2
e 1◆ e i: ə ɜː e I 2◆ ei ey ei i: 3◆ eer ɪə 4◆ ee i: 5◆ er ə 6◆ ere ɪə eə 7◆ ea ɪə i: ə ...
- 微信小程序中的bindTap事件(微信小程序开发QQ群:604788754)
bindTap对应的绑定事件, 第一个:wx.navigateTo wx.navigateTo({ url:"../content/content" }) 第二个:wx.redir ...
- RK3288 GMAC整理
一.源文件 源码路径:\drivers\net\ethernet\rockchip\gmac 源码阅读顺序: 二.重要探针函数stmmac_dvr_probe 1. alloc_etherdev 申请 ...
- oracle不小心更新了数据库中的所有内容后的恢复
开发过程中,在更新数据库中的某一条数据时,由于疏忽忘记加where判断条件了,这时更新会提示是否要更新全部数据,但是由于自己没有仔细看提示导致直接点确定并commit了,导致数据库中所有数据的相关字段 ...
- OSI七层网络模型与TCP/IP四层模型介绍
目录 OSI七层网络模型与TCP/IP四层模型介绍 1.OSI七层网络模型介绍 2.TCP/IP四层网络模型介绍 3.各层对应的协议 4.OSI七层和TCP/IP四层的区别 5.交换机工作在OSI的哪 ...
- Redis 缓存服务器
Redis 服务器 Remote Dictionay Server Redis是一个key-value持久化产品,通常被称为数据结构服务器. Redis的key是string类型:value可以是 ...
- 微信小程序插件开发
小程序插件功能介绍 插件,是可被添加到小程序内直接使用的功能组件.开发者可以像开发小程序一样开发一个插件,供其他小程序使用.同时,小程序开发者可直接在小程序内使用插件,无需重复开发,为用户提供更丰富的 ...
- OpenCV代码提取:遍历指定目录下指定文件的实现
前言 OpenCV 3.1之前的版本,在contrib目录下有提供遍历文件的函数,用起来比较方便.但是在最新的OpenCV 3.1版本给去除掉了.为了以后使用方便,这里将OpenCV 2.4.9中相关 ...
- PHP webservice初探
背景:在最近的开发中,为了解决公司内部系统与外部系统的对接,开始接触到了webservice接口,外部公司提供接口供我们调用,已达到数据同步的目的,因此有必要普及一下web service的知识了! ...