1. python中有一個第三方庫叫'requests'中提供了所有和http請求相關(guān)的函數(shù)
1.1 get請求
????get(url, params=None) - 發(fā)送請求獲取服務(wù)器返回的響應
????url - 請求地址, 字符串
????params - 請求參數(shù), 字典
方法一: (既適用于get也適用于post)
????url = 'https://www.apiopen.top/satinApi'
????params = {'type': 1, 'page': 2}
????response = requests.get(url, params)
????print(response)
方法二: 只能用于get請求
????url = 'https://www.apiopen.top/satinApi?type=1&page=1'
????response = requests.get(url)
????# print(response)
1.3 獲取請求結(jié)果
響應頭
{'Server': 'nginx', 'Date': 'Thu, 15 Aug 2019 03:39:09 GMT', 'Content-Type': 'application/json;charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, DELETE', 'Access-Control-Max-Age': '3600', 'Access-Control-Allow-Headers': 'x-requested-with'}
print(response.headers)
響應體(數(shù)據(jù))
a. 獲取二進制對應的原數(shù)據(jù)(數(shù)據(jù)本身是圖片紫皇、壓縮文件窗轩、視頻等文件數(shù)據(jù))
????content = response.content
????print(type(content))
b. 獲取字符類型的數(shù)據(jù)
????text = response.text
????print(type(text))
c. 獲取json數(shù)據(jù)(json轉(zhuǎn)換成python對應的數(shù)據(jù))
????json = response.json()
????print(type(json))
????print(json)
import requests
# 方法一: (既適用于get也適用于post)
url = 'https://www.apiopen.top/satinApi'
params = {'type': 1, 'page': 2}
response = requests.get(url, params)
print(response)
# 方法二: 只能用于get請求
url = 'https://www.apiopen.top/satinApi?type=1&page=1'
response = requests.get(url)
# print(response)
print(response.headers)
# 2)響應體(數(shù)據(jù))
# a.獲取二進制對應的原數(shù)據(jù)(數(shù)據(jù)本身是圖片栏豺、壓縮文件湿蛔、視頻等文件數(shù)據(jù))
content = response.content
print(type(content))
# b.獲取字符類型的數(shù)據(jù)
text = response.text
print(type(text))
# c.獲取json數(shù)據(jù)(json轉(zhuǎn)換成python對應的數(shù)據(jù))
json = response.json()
print(type(json))
print(json)
2. 進程
????每個進程默認都有一條線程抵知,這個線程叫主線程刷喜。其他線程叫子線程
????threading模塊中Thread的對象就是線程對象,當程序中需要子線程就創(chuàng)建Thread類的對象
import threading
from datetime import datetime
from time import sleep
def download(film_name):
# film_name = '魔童降世'
print('開始下載%s:%s' % (film_name, datetime.now()))
print(film_name, threading.current_thread())
sleep(5)
print('%s下載結(jié)束:%s' % (film_name, datetime.now()))
if __name__ == '__main__':
# download('魔童降世')
# download('掃毒2')
# download('怦然心動')
# 1.創(chuàng)建線程對象
"""
Thread(target=None,args=()) - 創(chuàng)建并且返回一個子線程對象
target - 函數(shù)類型(function), 在線程啟動的時候這個函數(shù)會在子線程中執(zhí)行
args - 元祖,元祖中的元素就是target對應的函數(shù)在子線程中調(diào)用的時候傳的實參
"""
t1 = threading.Thread(target=download, args=('魔童降世',))
t2 = threading.Thread(target=download, args=('掃毒2',))
t3 = threading.Thread(target=download, args=('怦然心動',))
print(threading.current_thread())
# 2.啟動線程
"""
線程對象.start() - 讓線程去執(zhí)行線程中的任務(wù)
target(*args)
"""
t1.start()
t2.start()
t3.start()
3. 程序結(jié)束
????線程中的任務(wù)執(zhí)行完成線程就結(jié)束; 程序出現(xiàn)異常結(jié)束的是線程恼布,不是進程
????進程中的所有線程就結(jié)束進程才結(jié)束;
3.1 聲明一個類繼承Thread
3.2 實現(xiàn)類中的run方法折汞,這個方法中的代碼就是需要在子線程中執(zhí)行的代碼
3.3 需要子線程的時候就創(chuàng)建自己聲明的類的對象爽待,并且不需要任何參數(shù)
from threading import *
from datetime import datetime
from time import sleep
class DownloadThread(Thread):
def __init__(self, film_name):
super().__init__()
self.film_name = film_name
def run(self) -> None:
print('開始下載%s:%s' % (self.film_name, datetime.now()))
print(current_thread())
sleep(5)
print('%s下載結(jié)束:%s' % (self.film_name, datetime.now()))
if __name__ == '__main__':
t1 = DownloadThread('魔童降世')
t2 = DownloadThread('掃毒')
t1.start()
t2.start()
print([1, 2, 3][10])
print('==============')
4. join的用法
????線程對象.join() - 當前線程對象任務(wù)執(zhí)行完成后才能去執(zhí)行后面的代碼
from threading import *
from datetime import datetime
from time import *
from random import randint
def download(film_name):
print('開始下載%s:%s' % (film_name, datetime.now()))
sleep(randint(2, 7))
print('%s下載結(jié)束:%s' % (film_name, datetime.now()))
if __name__ == '__main__':
# 1.join的用法
"""
線程對象.join() - 當前線程對象任務(wù)執(zhí)行完成后才能去執(zhí)行后面的代碼
"""
t1 = Thread(target=download, args=('魔童降世',))
t2 = Thread(target=download, args=('掃毒2',))
t3 = Thread(target=download, args=('怦然心動',))
# 情況一:三個電影都下載完成才執(zhí)行'下載完成!!!'
# t1.start()
# t2.start()
# t3.start()
#
# t1.join()
# t2.join()
# t3.join()
# print('下載完成!!!!')
# 情況二: 電影1下載完成后才開始同時下載電影2和電影3
t1.start()
t1.join()
t2.start()
t3.start()