urllib.request 的学习

urllib.request的应用

ps:此次学习都没封装自己的信息,光是为了学习命令。

获取一个get请求

测试网页:http://www.baidu.com

# -*- coding: UTF-8 -*-
# @Time : 2021/5/11 20:27
# @Author : 李如旭
# @File :testUrllib.py
# @Software: PyCharm


import  urllib.request

response = urllib.request.urlopen("http://www.baidu.com")
print(response.read().decode('utf-8'))

获取效果
在这里插入图片描述
打开效果(无图片)功能部份正常
在这里插入图片描述

获取一个post请求

测试网页:http://httpbin.org/

目标网页反馈码

http://httpbin.org/post(post形式)
在这里插入图片描述

代码

# -*- coding: UTF-8 -*-
# @Time : 2021/5/11 20:27
# @Author : 李如旭
# @File :testUrllib.py
# @Software: PyCharm


import urllib.request


#获取一个get请求
# response = urllib.request.urlopen("http://www.baidu.com")
# print(response.read().decode('utf-8'))

#获取一个post请求

import urllib.parse

data = bytes(urllib.parse.urlencode({"hello":"world"}),encoding="utf-8")
response =urllib.request.urlopen("http://httpbin.org/post",data=data)
print(response.read().decode("utf-8"))

运行效果比对目标效果 结果一样     So    完成任务!!
在这里插入图片描述

超时处理

以get方法为例

1s没响应即报异常

# -*- coding: UTF-8 -*-
# @Time : 2021/5/11 20:27
# @Author : 李如旭
# @File :testUrllib.py
# @Software: PyCharm


import urllib.request


#获取一个get请求
# response = urllib.request.urlopen("http://www.baidu.com")
# print(response.read().decode('utf-8'))

#获取一个post请求

# import urllib.parse
#
# data = bytes(urllib.parse.urlencode({"hello":"world"}),encoding="utf-8")
# response =urllib.request.urlopen("http://httpbin.org/post",data=data)
# print(response.read().decode("utf-8"))


#超时处理
try:
    response =urllib.request.urlopen("http://httpbin.org/get",timeout=1)
    print(response.read().decode("utf-8"))
except urllib.error.URLEerror as e:
    print("time out!")

response.getheaders()

读取除网页header部分

获取所有信息的代码

# -*- coding: UTF-8 -*-
# @Time : 2021/5/11 20:27
# @Author : 李如旭
# @File :testUrllib.py
# @Software: PyCharm


import urllib.request


#获取一个get请求
# response = urllib.request.urlopen("http://www.baidu.com")
# print(response.read().decode('utf-8'))

#获取一个post请求

# import urllib.parse
#
# data = bytes(urllib.parse.urlencode({"hello":"world"}),encoding="utf-8")
# response =urllib.request.urlopen("http://httpbin.org/post",data=data)
# print(response.read().decode("utf-8"))


# #超时处理
# try:
#     response =urllib.request.urlopen("http://httpbin.org/get",timeout=1)
#     print(response.read().decode("utf-8"))
# except urllib.error.URLEerror as e:
#     print("time out!")


#读取单个内容

response =urllib.request.urlopen("http://www.baidu.com",timeout=1)
print(response.getheaders())

代码实现
在这里插入图片描述

单单获得一个信息 比如Bdpagetype

代码

# -*- coding: UTF-8 -*-
# @Time : 2021/5/11 20:27
# @Author : 李如旭
# @File :testUrllib.py
# @Software: PyCharm


import urllib.request


#获取一个get请求
# response = urllib.request.urlopen("http://www.baidu.com")
# print(response.read().decode('utf-8'))

#获取一个post请求

# import urllib.parse
#
# data = bytes(urllib.parse.urlencode({"hello":"world"}),encoding="utf-8")
# response =urllib.request.urlopen("http://httpbin.org/post",data=data)
# print(response.read().decode("utf-8"))


# #超时处理
# try:
#     response =urllib.request.urlopen("http://httpbin.org/get",timeout=1)
#     print(response.read().decode("utf-8"))
# except urllib.error.URLEerror as e:
#     print("time out!")


#读取单个内容

response =urllib.request.urlopen("http://www.baidu.com",timeout=1)
print(response.getheader("Bdpagetype"))

代码实现

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值