一、http請求
Python中有一個第三方庫叫‘request’中提供了所有和http請求相關(guān)的函數(shù)
1寒波、get請求
get(url, params=None) ——發(fā)送請求獲取服務(wù)器返回的響應(yīng)
url ——請求地址鞋真,字符串
params ——請求參數(shù)崇堰,字典
2、獲取請求結(jié)果
1)響應(yīng)頭
2)響應(yīng)體(數(shù)據(jù))
a、獲取二進(jìn)制對應(yīng)的原數(shù)據(jù)(數(shù)據(jù)本身是圖片海诲、壓縮文件繁莹、視頻等文件數(shù)據(jù))
b、獲取字符類型的數(shù)據(jù)
c特幔、獲取json數(shù)據(jù)(json轉(zhuǎn)換成Python對應(yīng)的類型
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)
# 2.獲取請求結(jié)果
# 1)響應(yīng)頭
print(response.headers)
# 2)響應(yīng)體(數(shù)據(jù))
# a.獲取二進(jìn)制對應(yīng)的原數(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對應(yīng)的數(shù)據(jù))
json = response.json()
print(type(json))
print(json)
二蚯斯、多線程基礎(chǔ)1
"""
1雪标、線程
每個進(jìn)程默認(rèn)都有一條線程,這個線程叫主線程溉跃,其他叫子線程
threading模塊中Thread的對象就是線程對象,當(dāng)程序中需要子線程就創(chuàng)建Thread類的對象
1)—創(chuàng)建線程對象:
Thread(target=None, args=()) —創(chuàng)建并且返回一個子線程對象
target —函數(shù)類型(function)告抄,在線程啟動的時候這個函數(shù)會在子線程中執(zhí)行
args —元祖撰茎,元祖中的元素就是target對應(yīng)的函數(shù)在子線程中調(diào)用的時候傳的實參
2)—啟動線程
線程對象.start() —讓線程去執(zhí)行線程中的任務(wù)
target(*args)
import threading
from datetime import datetime
from time import sleep
def download(film_name):
print('開始下載%s:%s' % (film_name, datetime.now()))
sleep(5)
print('下載結(jié)束%s:%s' % (film_name, datetime.now()))
if __name__ == '__main__':
t1 = threading.Thread(target=download, args=('魔童降世',))
t2 = threading.Thread(target=download, args=('掃毒2',))
t3 = threading.Thread(target=download, args=('阿凡達(dá)',))
t1.start()
t2.start()
t3.start()
三、多線程基礎(chǔ)2
1打洼、申明一個類繼承Thread
2龄糊、實現(xiàn)類中的run方法,這個方法中的代碼就是需要在子線程中執(zhí)行的代碼
3募疮、需要子線程的時候就創(chuàng)建自己申明的類的對象炫惩,并且不需要傳參
線程中的任務(wù)執(zhí)行完成線程就結(jié)束
進(jìn)程中的所有進(jìn)程都結(jié)束進(jìn)程才結(jié)束
程序出現(xiàn)異常結(jié)束的是線程,不是進(jìn)程
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()))
sleep(5)
print('下載結(jié)束%s:%s' % (self.film_name, datetime.now()))
if __name__ == '__main_':
t1 = DownloadThread('魔童降世')
t2 = DownloadThread('追龍2')
t1.start()
t2.start()
四阿浓、join用法
from threading import *
from datetime import datetime
from time import *
from random import *
def download(file_name):
print('開始下載%s:%s', file_name, datetime.now())
sleep(randint(3, 7))
print('下載結(jié)束%s:%s', file_name, datetime.now())
if __name__ == '__main__':
# join的用法
"""
線程對象.join() —當(dāng)前線程對象任務(wù)執(zhí)行完成后才能去執(zhí)行后面的代碼
"""
t1 = Thread(target=download, args=('魔童降世',))
t2 = Thread(target=download, args=('掃毒2',))
t3 = Thread(target=download, args=('阿凡達(dá)',))
t1.start()
t2.start()
t3.start()
# 三個電影都下載完成才執(zhí)行打印‘全部下載完成’
t1.join() # 可以用在不同的位置來確定執(zhí)行順序
t2.join()
t3.join()
print('全部下載完成')