官方文档

官方文档: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的更多相关文章

  1. 爬虫小探-Python3 urllib.request获取页面数据

    使用Python3 urllib.request中的Requests()和urlopen()方法获取页面源码,并用re正则进行正则匹配查找需要的数据. #forex.py#coding:utf-8 ' ...

  2. 【转】python3 urllib.request 网络请求操作

    python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...

  3. Python3 urllib.request库的基本使用

    Python3 urllib.request库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 在Python中有很多库可以用来抓取网页,我们先学习urlli ...

  4. python3 urllib 类

    urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google ...

  5. python3 urllib.request 网络请求操作

    python3 urllib.request 网络请求操作 基本的网络请求示例 ''' Created on 2014年4月22日 @author: dev.keke@gmail.com ''' im ...

  6. Python3 urllib.parse 常用函数示例

    Python3 urllib.parse 常用函数示例 http://blog.51cto.com/walkerqt/1766670  1.获取url参数. >>> from url ...

  7. python2 与 python3 urllib的互相对应关系

    urllib Python2 name Python3 nameurllib.urlopen() Deprecated. See urllib.request.urlopen() which mirr ...

  8. python3 urllib.request.urlopen() 地址打开错误

    错误内容:UnicodeEncodeError: 'ascii' codec can't encode characters in position 28-29: ordinal not in ran ...

  9. python3 urllib及requests基本使用

    在python中,urllib是请求url连接的标准库,在python2中,分别有urllib和urllib,在python3中,整合成了一个,称谓urllib 1.urllib.request re ...

  10. (转)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 ...

随机推荐

  1. qScrollArean的使用

    1◆ qScrollArean的使用   qt designer 工具 有时会 卡死的   2◆ 展示效果   滚动条   3◆ 操作   4◆ 说明 qt designer会卡死的    

  2. 常用docker

    随便什么版本的linux 接入daocloud.io 在发现镜像中选择DockerHub 搜索对应的image,然后部署. 手动输入YAML即可 aria: image: cuteribs/aria2 ...

  3. 玩转X-CTR100 l STM32F4 l 红外遥控接收

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      X-CTR100控制器具有红外接收头,例程 ...

  4. MyEclipse移动开发教程:设置所需配置的iOS应用(三)

    MyEclipse个人授权 折扣低至冰点!立即开抢>> [MyEclipse最新版下载] 三.创建配置文件 Provisioning profiles授权文件应用程序在iOS设备上安装并运 ...

  5. dubbo集群应用

    前面写了一篇dubbo的基础应用篇,单机版 http://www.cnblogs.com/yun965861480/p/6257670.html, 这次将集群的相关配置记录下来. 1.zookeepe ...

  6. Redis 学习第一课:安装Linux Redis(Ubantu)

    对于分布式缓存,之前公司项目中只使用了MemCached,使用比较方便,有现成的C#版本组件. 如今用Redis的公司有很多,所以打算了解一下Redis. Redis的官网地址:http://redi ...

  7. android:Unsupported major.minor version 52.0

    错误描述 使用android studio项目编译过程报如下错误: java.lang.UnsupportedClassVersionError: com/android/build/gradle/A ...

  8. 题目1001:A+B for Matrices

    题目1001:A+B for Matrices 时间限制:1 秒内存限制:32 兆 题目描述: This time, you are supposed to find A+B where A and  ...

  9. SecureCRT来上传和下载文件

    引用:https://www.cnblogs.com/zhengyihan1216/p/6260667.html Linux--用SecureCRT来上传和下载文件 SecureCRT下的文件传输协议 ...

  10. UnicodeDammit

    UnicodeDammit 是BS内置库, 主要用来猜测文档编码. 编码自动检测 功能可以在Beautiful Soup以外使用,检测某段未知编码时,可以使用这个方法: from bs4 import ...