python3使用自带urllib发送http请求实例

简述

本文主要使用python3 自带的 urllib实现发送GET请求。并将结果状态,值输出。
urllib 是一个收集了多个涉及 URL 的模块的包:

  • urllib.request 打开和读取 URL

  • urllib.error 包含 urllib.request 抛出的异常

  • urllib.parse 用于解析 URL

  • urllib.robotparser 用于解析 robots.txt 文件

示例代码

import urllib.request

response = urllib.request.urlopen("https://www.jd.com")

print("response 的类型:", type(response))
print("status:", response)
print("status : |%s msg: %s |version: %s" % (response.status, response.msg, response.version))
# headers
print("headers:%s" % response.getheaders())
# 输出html代码 
print(response.read().decode('utf-8'))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章