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'))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章