1、什麼是Urllib?socket
Python內置的HTTP請求庫post
urllib.request 請求模塊url
urllib.error 異常處理模塊spa
urllib.parse url解析模塊code
urllib.robotparser robots.txt解析模塊blog
2、相比Python2變化utf-8
Python2get
import urllib2class
response = urllib2.urlopen('http://www.baidu.com')import
Python3
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
#urlopen #一、get請求 import urllib.request response = urllib.request.urlopen('http://www.baidu.com') print(response.read().decode('utf-8')) #二、post請求 import urllib.request import urllib.parse data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf-8') response = urllib.request.urlopen('http://httpbin.org/post',data=data) print(response.read().decode('utf-8')) #三、請求超時 import urllib.request import urllib.error import socket try: response = urllib.request.urlopen('http://httpbin.org/get',timeout=0.1) except urllib.error.URLError as e: if isinstance(e.reason,socket.timeout): print('TIME OUT') #響應 #一、響應類型 import urllib.request response = urllib.request.urlopen('http://www.baidu.com') type(response) #>><class 'http.client.HTTPResponse'> #二、狀態碼,響應頭(返回爲列表類型),獲取響應頭中對應的信息 print(response.status) #>>200 print(response.getheaders()) print(response.getheader('Server')) #三、響應體內容,返回的是二進制形式 response.read() #Request類 from urllib import request,parse url = "http://httpbin.org/post" headers = {} dict = { 'name':'Tim' } data = bytes(parse.urlencode(dict),encoding='utf8') req = request.Request(url=url,headers=headers,data=data,method='POST') response = request.urlopen(req) print(response.read().decode('utf-8'))