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基本库的使用的更多相关文章

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

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

  2. 爬虫——urllib.request库的基本使用

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

  3. 爬虫入门【1】urllib.request库用法简介

    urlopen方法 打开指定的URL urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, ca ...

  4. python爬虫03:那个叫做 Urllib 的库让我们的 python 假装是浏览器

    相信你已经摸清了 浏览器各种请求的套路 也知道了怎么在手机上进行请求和返回数据的抓取 那么接下来我们就开始来使用 python 了 代码 lu 起来 那么 怎么用 python 写各种请求呢? 今天要 ...

  5. python3.6 urllib.request库实现简单的网络爬虫、下载图片

    #更新日志:#0418 爬取页面商品URL#0421 更新 添加爬取下载页面图片功能#0423 更新 添加发送邮件功能# 优化 爬虫异常处理.错误页面及空页面处理# 优化 爬虫关键字黑名单.白名单,提 ...

  6. Python 的 urllib.parse 库解析 URL

      Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url urlparse() 函数可以将 URL 解析成 ParseResult 对象.对象中包含了六 ...

  7. 通过python的urllib.request库来爬取一只猫

    我们实验的网站很简单,就是一个关于猫的图片的网站:http://placekitten.com 代码如下: import urllib.request respond = urllib.request ...

  8. Python爬虫入门:Urllib parse库使用详解(二)

    文字转载:https://www.jianshu.com/p/e4a9e64082ef,转载内容仅供学习 如有侵权,请联系删除 获取url参数 urlparse 和 parse_qs ParseRes ...

  9. python爬虫---urllib库的基本用法

    urllib是python自带的请求库,各种功能相比较之下也是比较完备的,urllib库包含了一下四个模块: urllib.request   请求模块 urllib.error   异常处理模块 u ...

随机推荐

  1. flask+gevent的异步框架

    一:flask本身的框架时什么? 基于Wsgi的Web应用框架 二:为什么要实现异步架构? 增加并发处理能力 三:实现异步架构 from gevent import monkey from geven ...

  2. 常见状态码StatusCode

    当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应浏览器的请求. ...

  3. 8.0.17 MySQL Community Server 二进制手工安装

    8.0.17 MySQL Community Server 二进制手工安装 环境简介 操作系统:Centos 6.10 64位目前版本:8.0.17 MySQL Community Server 二进 ...

  4. 关于antd表单的自行校验

    rules里面加上validator验证,value就是输入的值 上面为正则表达式的检验

  5. python3中OpenCV imwrite保存中文路径文件

    原先一段将特征值保存为图片的代码,这部分学生的电脑上运行没有生成图片 代码的基本样子是: import os import cv2 import numpy as np def text_to_pic ...

  6. spring mvc @Valid 数据验证

    //对书的单价校验不能是空,价格在20-100之间   @DecimalMax(value = "100", message = "价格不超过100元")   ...

  7. 神经网络内在逻辑:没打开的AI“黑匣子”

    转载自:http://www.elecfans.com/rengongzhineng/592200.html 伴随着大数据,AI在沉寂了多年之后,又迎来了新的高潮.在这场涉及大部分科学的革命中,人工神 ...

  8. springboot(一).初识springboot以及基本项目搭建

    初识springboot 以及基本项目搭建 由于新的项目需要搭建后台框架,之前的springmvc架构也使用多次,在我印象中springboot的微服务架构更轻量级更容易搭建,所以想去试试spring ...

  9. 【转】毛虫算法——尺取法

    转自http://www.myexception.cn/program/1839999.html 妹子满分~~~~ 毛毛虫算法--尺取法 有这么一类问题,需要在给的一组数据中找到不大于某一个上限的&q ...

  10. Updatexml函数再mysql中的作用

    函数的解释 http://www.blogjava.net/chenpengyi/archive/2006/07/11/57578.html 我的理解就是updatexml函数具有查询功能 并且会再x ...