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. rsync服务端排错思路

    rsync服务端排错思路       rsync服务端排错思路 查看rsync服务配置文件路径是否正确,正确的默认路径为/etc/rsyncd.conf 查看配置文件里host allow,host ...

  2. sqlserver表值函数调用方式

    Connection conn = sqlServerManage.sqlServerConn(); Statement stmt; ResultSet rs; // 组装sql StringBuff ...

  3. redis集群搭建_超详细

    redis集群中至少应该有三个节点,以保证当集群中的某个节点挂掉,其他节点进行容错投票时,投票数能超过半票. 要保证高可用,则还需要每一个节点有一个备份机. 因此redis集群至少需要6台服务器.这里 ...

  4. Python使用XML操作mapnik,实现复杂标注(Multi line text symbolizer)

    test.py import mapnik stylesheet = 'world_style.xml' image = 'world_style.png' m = mapnik.Map(1200, ...

  5. The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs

    题目:https://nanti.jisuanke.com/t/41399 思路:差分数组 区间内操作次数为奇数次则灯为打开状态 #include<bits/stdc++.h> using ...

  6. MySQL5.7数据转移至SQL Server详解

    本文链接:https://blog.csdn.net/qq_37308779/article/details/80679358一.安装MySQL ODBC驱动为MySQL安装Connector/ODB ...

  7. 把网站从 http 转换成 https

    基础准备: 一台服务器,一个主域名或多级域名,本次申请的免费 本次环境使用 centos6.5 + nginx1.8 + jdk1.8 + tomcat8 如果需要收费的请参考: 云盾证书服务(包年) ...

  8. ClientScriptManager.RegisterClientScriptBlock Method 无效

    ClientScriptManager.RegisterClientScriptBlock Method 这个方法不能在Render方法里面使用,但是可以在PreRender中使用 最好是放到OnLo ...

  9. What's the difference between HEAD^ and HEAD~ in Git?

    https://stackoverflow.com/questions/2221658/whats-the-difference-between-head-and-head-in-git Rules ...

  10. linux交叉编译Windows版本的ffmpeg

    主要参考http://www.cnblogs.com/haibindev/archive/2011/12/01/2270126.html 在我的机器上编译libfaac的时候 出现问题了 输出如下 . ...