urllib基本库的使用
get方法的学习
1import urllib.request
2import ssl
3#设置全局证书
4ssl._create_default_https_context = ssl._create_unverified_context
5response = urllib.request.urlopen("https://www.python.org/getit/")
6print(response.read().decode('utf-8'))
7print(type(response))
8#响应状态码
9print(response.status)
10#响应头
11print(response.getheaders())
12#响应头中server的值
13print(response.getheader("Server"))
14#响应的msg值
15print(response.msg)
post的方法
1import urllib.parse
2import urllib.request
3import socket
4import urllib.error
5'''byte方法 将参数转化为字节流'''
6def studyData():
7 data = bytes(urllib.parse.urlencode({'word': 'hello'}), encoding='utf8')
8 print(data)
9 '''data参数'''
10 response = urllib.request.urlopen("http://httpbin.org/post", data=data)
11 print(response.read().decode('utf-8'))
12'''timeout参数'''
13
14def studyTimeout():
15 try:
16 response1 = urllib.request.urlopen("http://httpbin.org/get", timeout=0.1)
17 print(response1)
18 except urllib.error.URLError as e:
19 if isinstance(e.reason, socket.timeout):
20 print('ITME OUT')
21
22studyData()
23studyTimeout()
request请求对象1
1import urllib.request
2import ssl
3ssl._create_default_https_context = ssl._create_unverified_context
4
5request = urllib.request.Request('https://www.python.org')
6response = urllib.request.urlopen(request)
7print(response.read().decode('utf-8'))
request请求对象2
1from urllib import request,parse
2import ssl
3ssl._create_default_https_context = ssl._create_unverified_context
4
5url = 'https://www.python.org/post'
6headers = {
7 'User-Agent': 'Mozilla/4.0(compile;MSIE 5.5;Window NT)',
8 'Host': 'httpbin.org',
9}
10dict = {
11 'name': 'Germey'
12}
13data = bytes(parse.urlencode(dict), encoding='utf8')
14req = request.Request(url=url, data=data, headers=headers, method='POST')
15response = request.urlopen(req)
16print(response.read().decode('utf8'))
17
18req =request.Request(url=url, data=data, method='POST')
19req.add_header('User-Agent', 'Mozilla/4.0(compile;MSIE 5.5;Window NT)')
urllib基本库的使用的更多相关文章
- Python3 urllib.request库的基本使用
Python3 urllib.request库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 在Python中有很多库可以用来抓取网页,我们先学习urlli ...
- 爬虫——urllib.request库的基本使用
所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地.在Python中有很多库可以用来抓取网页,我们先学习urllib.request.(在python2.x中为urllib2 ...
- 爬虫入门【1】urllib.request库用法简介
urlopen方法 打开指定的URL urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, ca ...
- python爬虫03:那个叫做 Urllib 的库让我们的 python 假装是浏览器
相信你已经摸清了 浏览器各种请求的套路 也知道了怎么在手机上进行请求和返回数据的抓取 那么接下来我们就开始来使用 python 了 代码 lu 起来 那么 怎么用 python 写各种请求呢? 今天要 ...
- python3.6 urllib.request库实现简单的网络爬虫、下载图片
#更新日志:#0418 爬取页面商品URL#0421 更新 添加爬取下载页面图片功能#0423 更新 添加发送邮件功能# 优化 爬虫异常处理.错误页面及空页面处理# 优化 爬虫关键字黑名单.白名单,提 ...
- Python 的 urllib.parse 库解析 URL
Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url urlparse() 函数可以将 URL 解析成 ParseResult 对象.对象中包含了六 ...
- 通过python的urllib.request库来爬取一只猫
我们实验的网站很简单,就是一个关于猫的图片的网站:http://placekitten.com 代码如下: import urllib.request respond = urllib.request ...
- Python爬虫入门:Urllib parse库使用详解(二)
文字转载:https://www.jianshu.com/p/e4a9e64082ef,转载内容仅供学习 如有侵权,请联系删除 获取url参数 urlparse 和 parse_qs ParseRes ...
- python爬虫---urllib库的基本用法
urllib是python自带的请求库,各种功能相比较之下也是比较完备的,urllib库包含了一下四个模块: urllib.request 请求模块 urllib.error 异常处理模块 u ...
随机推荐
- Linux系统中的硬件问题如何排查?(4)
Linux系统中的硬件问题如何排查?(4) 2013-03-27 10:32 核子可乐译 51CTO.com 字号:T | T 在Linux系统中,对于硬件故障问题的排查可能是计算机管理领域最棘手的工 ...
- windows补丁服务器
一.WSUS 安装要求 1.硬件要求: 对于多达 13000 个客户端的服务器,建议使用以下硬件:* 4 Core E5-2609 2.1GHz 的处理器* 8 GB 的 RAM 2.软件要求: 要使 ...
- C#与JAVA的互通
C#与JAVA的互通 https://blog.csdn.net/sinat_35165183/article/details/68936910
- Shell-06 函数
Shell-06 函数 #编写脚本,使用chkconfig命令,循环执行,关闭所有5级别服务 #!/bin/bash name=`chkconfig --list | cut -d' ' -f1` f ...
- UNIX环境C - 系统信号
一.信号的概念 信号就是一种软中断,进程与进程之间信号的传递,都是通过内核来当做中转站的,不能直接传递信号. 二.信号的分类(128位信号,不过可用信号就1~64除去32与33) 1.不可靠信号(SI ...
- MongoDB操作:update()
@Override public boolean update(String dbName, String collectionName, DBObject oldValue, DBObject ne ...
- SVG相关学习(一)SVG基础
SVG 相关学习 SVG SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG viewBox <svg width="500" heigh ...
- 《Effective Java》读书笔记 - 5.泛型
Chapter 5 Generics Item 23: Don't use raw types in new code 虽然你可以把一个List<String>传给一个List类型(raw ...
- 【零售App】—— react/ant design mobile项目爬坑
一.H5制作 - 图片文本的动画效果 bug:打开一个模板,添加图片,添加动画效果,若先选定动画效果,再调节动画时间和延迟时间,则动画和延迟时间没有改变:若先调节动画时间和延迟时间在选定动画效果,则动 ...
- 解决Python print输出不换行没空格的问题
今天在做编程题的时候发现Python的print输出默认换行输出,并且输出后有空格. 题目要求输出 122 而我的输出是: 1 2 2 于是我百度查到取消print自动换行的方法:就是在print的值 ...