Urllib库


python内置的http请求库

1、urllib.request 请求模块

2、urllib.error 异常处理模块(try,catch)

3、urllib.parse url解析模块

4、urllib.robotparser robots.txr解析模块


urlopen

get请求

import urllib.request
reponse=urllib.request.urlopen("http://www.baidu.com")
print(reponse.read().decode('utf-8'))#.read()读请求内容

post请求

import urllib.parse#貌似加不加都行
import urllib.request
data=bytes(urllib.parse.urlencode({'name':'汪国强'}),encoding='utf-8')
response=urllib.request.urlopen('http://httpbin.org/post',data=data)
print(response.read().decode('utf-8'))

urllib.error

import urllib.request
import socket
import urllib.error
try:
response=urllib.request.urlopen('http://httpbin.org/get',timeout=0.01)
except urllib.error.URLError as e: #超时属于URLError
if isinstance(e.reason,socket.timeout):
print('timeout')

对响应的一些处理

状态码、响应头

import urllib.request
import socket
import urllib.error
response=urllib.request.urlopen('http://www.baidu.com')
print(response.status)
print('-----------------')
print(response.getheaders())
print('-----------------')
print(response.getheader('Server'))

得到:

200        状态码
-----------------

响应头
[('Date', 'Mon, 25 Dec 2017 09:59:01 GMT'), ('Content-Type', 'text/html; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Connection', 'Close'), ('Vary', 'Accept-Encoding'), ('Set-Cookie', 'BAIDUID=C941C9CEBE13F4D6264663E5A10D4603:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), ('Set-Cookie', 'BIDUPSID=C941C9CEBE13F4D6264663E5A10D4603; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), ('Set-Cookie', 'PSTM=1514195941; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), ('Set-Cookie', 'BDSVRTM=0; path=/'), ('Set-Cookie', 'BD_HOME=0; path=/'), ('Set-Cookie', 'H_PS_PSSID=25394_1453_21119_25178_22157; path=/; domain=.baidu.com'), ('P3P', 'CP=" OTI DSP COR IVA OUR IND COM "'), ('Cache-Control', 'private'), ('Cxy_all', 'baidu+e8e6fa769a31bd4f787c267655da18e6'), ('Expires', 'Mon, 25 Dec 2017 09:58:11 GMT'), ('X-Powered-By', 'HPHP'), ('Server', 'BWS/1.1'), ('X-UA-Compatible', 'IE=Edge,chrome=1'), ('BDPAGETYPE', '1'), ('BDQID', '0xc958493100031c89'), ('BDUSERID', '0')]

-----------------

指定的响应头内容

BWS/1.1


如果想在请求时加上请求头怎么办?

import urllib.request
import urllib.parse
head={
"Host": "httpbin.org",
"Upgrade-Insecure-Requests": "",
}
dic={'name':''}
data=bytes(urllib.parse.urlencode(dic),encoding='utf-8')
request=urllib.request.Request('http://httpbin.org/post',data=data,headers=head,method='POST')
response=urllib.request.urlopen(request)
print(response.read().decode('utf-8'))

或者使用request.add_header()

import urllib.request,parser

dic={'name':''}
data=bytes(urllib.parse.urlencode(dic),encoding='utf-8')
request=urllib.request.Request('http://httpbin.org/post',data=data,method='POST')
request.add_header(
"Upgrade-Insecure-Requests", ""
)
response=urllib.request.urlopen(request)
print(response.read().decode('utf-8'))

Handler

代理

使用代理ip

import urllib.request
proxy_handler=urllib.request.ProxyHandler({
'http':'http://116.199.115.78:80/'
})
opener=urllib.request.build_opener(proxy_handler)
response=opener.open('http://httpbin.org/ip')
print(response.read().decode('utf-8'))

urllib(最基本的)库的应用的更多相关文章

  1. Python3 内置http.client,urllib.request及三方库requests发送请求对比

    如有任何学习问题,可以添加作者微信:lockingfree 更多学习资料请加QQ群: 822601020获取 HTTP,GET请求,无参 GET http://httpbin.org/get Pyth ...

  2. requests库和urllib包对比

    python中有多种库可以用来处理http请求,比如python的原生库:urllib包.requests类库.urllib和urllib2是相互独立的模块,python3.0以上把urllib和ur ...

  3. Python使用urllib,urllib3,requests库+beautifulsoup爬取网页

    Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...

  4. httplib、urllib、urllib2的区别

     Python3.4互联网通讯协议支持 1,webbrowser方便的浏览器容器 2,cgi公共网关接口支持 3,cgitb管理cgi脚本 4,wsgiref  WSGI实体和引用实现 5,urlli ...

  5. [转]Python中urllib与urllib2的区别与联系

    引用文章1:http://my.oschina.net/u/558071/blog/144792 引用文章2:http://zhuoqiang.me/python-urllib2-usage.html ...

  6. Python 学习之urllib模块---用于发送网络请求,获取数据

    1.urllib urllib是Python标准库的一部分,包含urllib.request,urllib.error,urllib.parse,urlli.robotparser四个子模块. (1) ...

  7. urllib、urllib2、urllib3区别和使用

    python3中把urllib和urllib合并为一个库了,urllib对应urllib.request 1.) python 中最早内置拥有的网络请求模块就是 urllib,我们可以看一下 urll ...

  8. python第三方库,你要的这里都有

    Python的第三方库多的超出我的想象. python 第三方模块 转 https://github.com/masterpy/zwpy_lst   Chardet,字符编码探测器,可以自动检测文本. ...

  9. Python 库,资源

    库名称简介 Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主要用于在终端或浏览器端构建格式 ...

随机推荐

  1. ora flashback详解

    使用oracle数据库时,难免会碰到一些问题. 例:1.如何回滚已经commit了的数据 2.如何查询已经被覆盖掉的数据[update],或者被delete了的数据 3.如何将数据恢复到某个时间点 我 ...

  2. mongo以及mysql常用语句

    db.user.find({']}}).pretty(); db.user.find({primaryPhone:).forEach(function(user){print('my phonenum ...

  3. csrf 攻击及防御

    1.什么是CSRF攻击: CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:C ...

  4. NuGet之控制台管理程序包

        NuGet作为VS的扩展程序,已经做好了UI,我们可以通过Manage NuGet Packages 的对话框.这里我们主要说说如何通过控制台进行包管理.使用命令行的方式,其实也是有其好处,对 ...

  5. 九度oj 题目1374:所有员工年龄排序

    题目描述: 公司现在要对所有员工的年龄进行排序,因为公司员工的人数非常多,所以要求排序算法的效率要非常高,你能写出这样的程序吗? 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入的第一行为一 ...

  6. 【bzoj1959】[Ahoi2005]LANE 航线规划 树链剖分+线段树

    题目描述 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由千百万星球构成的Samuel星系. 星际空间站的Samuel II巨型计算 ...

  7. 【Luogu】P3157动态逆序对(树状数组套主席树)

    题目链接 md第一道在NOILinux 下用vim做的紫题.由于我对这个操作系统不是很熟悉,似乎有什么地方搞错了,md调死.(我还打了两遍代码,调了两个小时) 但是这道题并不难,就是树状数组套上主席树 ...

  8. POJ——2236Wireless Network(暴力并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 22107   Accepted: 928 ...

  9. debug模式总是自动跳到ThreadPoolExecutor

    debug模式下eclipse总是自动跳到ThreadPoolExecutor解决方案 debug模式下eclipse总是自动跳到ThreadPoolExecutor解决方案 在eclipse中点击W ...

  10. 升级springboot 2.x 踩过的坑——跨域导致session问题

    目前IT界主流前后端分离,但是在分离过程中一定会存在跨域的问题. 什么是跨域? 是指浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域. 做过web后台的童鞋都知道,跨 ...