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
    评论
引用中的代码演示了使用urllib库发送HTTP请求的过程。在这段代码中,通过使用`urllib.request.urlopen`方法,将构造好的`request`请求作为参数发送给服务器,并获取到服务器的响应结果。 `urllib.request.urlopen(request)`这一行代码会发送HTTP请求,并返回一个响应对象`response`。通过调用`response.read()`方法可以获取响应的内容。在这段代码中,还使用了`decode('utf-8')`对响应内容进行解码,将其转换为字符串。 需要注意的是,在进行HTTP请求时,可以通过`urllib.request.Request`类来构造请求对象,可以在请求对象中设置请求的URL、请求方法、请求头等信息。在这段代码中,通过`urllib.request.Request('http://httpbin.org')`创建了一个请求对象,然后将其作为参数传递给`urllib.request.urlopen`方法。 另外,引用中提到了urllib库的一些基本概念和方法,可以帮助更好地理解和使用urllib库。而引用则是urllib.request官方文档的链接,其中提供了更详细的说明和示例,可以进一步查阅学习。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [urllib.request.urlopen详解](https://blog.csdn.net/qq_41845823/article/details/119465293)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [爬虫库Urlliburllib.request.Requesturllib.request.urlopen)](https://blog.csdn.net/qq_35194427/article/details/107617029)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值