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 ...
随机推荐
- C#窗体如何通过keybd_event()函数模拟键盘按键(组合键)产生事件
如何模拟键盘按键触发产生的事件,比如模拟按下Alt + F4 关闭当前程序,Ctrl+Shift 切换输入法等 可以通过win32api 键盘事件 keybd_event() 来实现 1.定义键盘按键 ...
- PHP:第一章——PHP中静态变量和常量
<?php header("Content-Type:text/html;charset=utf-8"); /******************************** ...
- POJ 3264 RMQ水题
题目大意就是有很多牛.告诉你每只牛的高度.然后有很多个询问.输出该区间内的最大身高差.也就是用RMQ求最大值最小值.貌似还可以用线段树.然而,我还不会线段树.....T_T 可能是太多组数据了.cin ...
- git HEAD游离状态问题解决
最近在迭代一个版本的时候,出现 HEAD detached at xxx 提示,应该是我切换分支的时候,哪里没弄对. 那么可以通过如下办法解决 git checkout 05 # 先checkou ...
- hdu4800 Josephina and RPG 解题报告
Josephina and RPG Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 反向-代理 nginx for Mac 的配置以及使用
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端 ...
- superset 安装配置
一.配置python虚拟环境,请参考 superset依赖python3.6环境 https://www.cnblogs.com/xibuhaohao/p/9994854.html 二.安装配置sup ...
- Flask初级(九)flash与前台交互get详解
Project name :Flask_Plan templates:templates static:static @app.route('/') def hello_world(): return ...
- css控制编辑器内容自动换行
在编辑器或者文本框中按住数字或字母不放 当字符很长时,就会撑破页面, 可以用一下方法控制字符自动换行 style="word-break:break-all;"
- 关于const的一些注意事项
1.const对象必须初始化,一旦创建其值就无法更改 2.默认情况下,const对象仅在文件内有效,若想在多个文件中使用同一个const对象,就是不在每个文件独立的定义一个const对象,可以进行如下 ...