Python多任務爬蟲實現(xiàn)爬取圖片和GDP數(shù)據(jù)
一. 基于FastAPI之Web站點開發(fā)
1. 基于FastAPI搭建Web服務器
# 導入FastAPI模塊
from fastapi import FastAPI
# 導入響應報文Response模塊
from fastapi import Response
# 導入服務器uvicorn模塊
import uvicorn
# 創(chuàng)建FastAPI框架對象
app = FastAPI()
# 通過@app路由裝飾器收發(fā)數(shù)據(jù)
# @app.get(參數(shù)) : 按照get方式接受請求數(shù)據(jù)
# 請求資源的 url 路徑
@app.get("/index.html")
def main():
with open("source/html/index.html", "rb") as f:
data = f.read()
# return 返回響應數(shù)據(jù)
# Response(content=data, media_type="text/html"
# 參數(shù)1: 響應數(shù)據(jù)
# 參數(shù)2: 數(shù)據(jù)格式
return Response(content=data, media_type="text/html")
# 運行服務器
# 參數(shù)1: 框架對象
# 參數(shù)2: IP地址
# 參數(shù)3: 端口號
uvicorn.run(app, host="127.0.0.1", port=8000)

2. Web服務器和瀏覽器的通訊流程
實際上Web服務器和瀏覽器的通訊流程過程并不是一次性完成的, 這里html代碼中也會有訪問服務器的代碼, 比如請求圖片資源。
那像0.jpg、1.jpg、2.jpg、3.jpg、4.jpg、5.jpg、6.jpg這些訪問來自哪里呢
答:它們來自index.html
3. 瀏覽器訪問Web服務器的通訊流程

瀏覽器訪問Web服務器的通訊流程:
瀏覽器(127.0.0.1/index.html) ==> 向Web服務器請求index.htmlWeb服務器(返回index.html) ==>瀏覽器瀏覽器解析index.html發(fā)現(xiàn)需要0.jpg ==>發(fā)送請求給Web服務器請求0.jpgWeb服務器收到請求返回0.jpg ==>瀏覽器接受0.jpg
通訊過程能夠成功的前提:
瀏覽器發(fā)送的0.jpg請求, Web服務器可以做出響應, 也就是代碼如下
# 當瀏覽器發(fā)出對圖片 0.jpg 的請求時, 函數(shù)返回相應資源
@app.get("/images/0.jpg")
def func_01():
with open("source/images/0.jpg", "rb") as f:
data = f.read()
print(data)
return Response(content=data, media_type="jpg")
4. 加載圖片資源代碼
# 導入FastAPI模塊
from fastapi import FastAPI
# 導入響應報文Response模塊
from fastapi import Response
# 導入服務器uvicorn模塊
import uvicorn
# 創(chuàng)建FastAPI框架對象
app = FastAPI()
@app.get("/images/0.jpg")
def func_01():
with open("source/images/0.jpg", "rb") as f:
data = f.read()
print(data)
return Response(content=data, media_type="jpg")
@app.get("/images/1.jpg")
def func_02():
with open("source/images/1.jpg", "rb") as f:
data = f.read()
return Response(content=data, media_type="jpg")
@app.get("/images/2.jpg")
def func_03():
with open("source/images/2.jpg", "rb") as f:
data = f.read()
return Response(content=data, media_type="jpg")
@app.get("/images/3.jpg")
def func_04():
with open("source/images/3.jpg", "rb") as f:
data = f.read()
return Response(content=data, media_type="jpg")
@app.get("/images/4.jpg")
def func_05():
with open("source/images/4.jpg", "rb") as f:
data = f.read()
return Response(content=data, media_type="jpg")
@app.get("/images/5.jpg")
def func_06():
with open("source/images/5.jpg", "rb") as f:
data = f.read()
return Response(content=data, media_type="jpg")
@app.get("/images/6.jpg")
def func_07():
with open("source/images/6.jpg", "rb") as f:
data = f.read()
return Response(content=data, media_type="jpg")
@app.get("/index.html")
def main():
with open("source/html/index.html", "rb") as f:
data = f.read()
# return 返回響應數(shù)據(jù)
# Response(content=data, media_type="text/source"
# 參數(shù)1: 響應數(shù)據(jù)
# 參數(shù)2: 數(shù)據(jù)格式
return Response(content=data, media_type="text/html")
# 運行服務器
# 參數(shù)1: 框架對象
# 參數(shù)2: IP地址
# 參數(shù)3: 端口號
uvicorn.run(app, host="127.0.0.1", port=8000)
二. 基于Web請求的FastAPI通用配置
1. 目前Web服務器存在問題
# 返回0.jpg
@app.get("/images/0.jpg")
def func_01():
with open("source/images/0.jpg", "rb") as f:
data = f.read()
print(data)
return Response(content=data, media_type="jpg")
# 返回1.jpg
@app.get("/images/1.jpg")
def func_02():
with open("source/images/1.jpg", "rb") as f:
data = f.read()
return Response(content=data, media_type="jpg")
# 返回2.jpg
@app.get("/images/2.jpg")
def func_03():
with open("source/images/2.jpg", "rb") as f:
data = f.read()
return Response(content=data, media_type="jpg")
對以上代碼觀察,會發(fā)現(xiàn)每一張圖片0.jpg、1.jpg、2.jpg就需要一個函數(shù)對應, 如果我們需要1000張圖片那就需要1000個函數(shù)對應, 顯然這樣做代碼的重復太多了.
2. 基于Web請求的FastAPI通用配置
# 當請求為 /images/0.jpg 時, path ==> 0.jpg
@app.get("/images/{path}")
# 注意這里的參數(shù)需要設置為 path
# path : str ==> 指定path為字符串類型的數(shù)據(jù)
def get_pic(path: str):
# 這里open()的路徑就是 ==> f"source/images/0.jpg"
with open(f"source/images/{path}", "rb") as f:
data = f.read()
# return 返回響應數(shù)據(jù)
# Response(content=data, media_type="jpg")
# 參數(shù)1: 響應數(shù)據(jù)
# 參數(shù)2: 數(shù)據(jù)格式
return Response(content=data, media_type="jpg")
完整代碼
# 導入FastAPI模塊
from fastapi import FastAPI
# 導入響應報文Response模塊
from fastapi import Response
# 導入服務器uvicorn模塊
import uvicorn
# 創(chuàng)建FastAPI框架對象
app = FastAPI()
# 當請求為 /images/0.jpg 時, path ==> 0.jpg
@app.get("/images/{path}")
# 注意這里的參數(shù)需要設置為 path
# path : str ==> 指定path為字符串類型的數(shù)據(jù)
def get_pic(path: str):
# 這里open()的路徑就是 ==> f"source/images/0.jpg"
with open(f"source/images/{path}", "rb") as f:
data = f.read()
# return 返回響應數(shù)據(jù)
# Response(content=data, media_type="jpg")
# 參數(shù)1: 響應數(shù)據(jù)
# 參數(shù)2: 數(shù)據(jù)格式
return Response(content=data, media_type="jpg")
@app.get("/{path}")
def get_html(path: str):
with open(f"source/html/{path}", 'rb') as f:
data = f.read()
# return 返回響應數(shù)據(jù)
# Response(content=data, media_type="text/source"
# 參數(shù)1: 響應數(shù)據(jù)
# 參數(shù)2: 數(shù)據(jù)格式
return Response(content=data, media_type="text/html")
# 運行服務器
# 參數(shù)1: 框架對象
# 參數(shù)2: IP地址
# 參數(shù)3: 端口號
uvicorn.run(app, host="127.0.0.1", port=8000)
運行結果
三. Python爬蟲介紹
1. 什么是爬蟲
網(wǎng)絡爬蟲:
又被稱為網(wǎng)頁蜘蛛,網(wǎng)絡機器人,是一種按照一定的規(guī)則,自動地抓取網(wǎng)絡信息的程序或者腳本,另外一些不常使用的名字還有螞蟻、自動索引、模擬程序或者蠕蟲。
通俗理解:
簡單來講,爬蟲就是一個探測機器,它的基本操作就是模擬人的行為去各個網(wǎng)站溜達,點點按鈕,查查數(shù)據(jù),或者把看到的信息背回來. 就像一只蟲子在一幢樓里不知疲倦地爬來爬去.
你可以簡單地想象: 每個爬蟲都是你的「分身」。就像孫悟空拔了一撮汗毛,吹出一堆猴子一樣
**百度:**其實就是利用了這種爬蟲技術, 每天放出無數(shù)爬蟲到各個網(wǎng)站,把他們的信息抓回來,然后化好淡妝排著小隊等你來檢索。
有了這樣的特性, 對于一些自己公司數(shù)據(jù)量不足的小公司, 這個時候還想做數(shù)據(jù)分析就可以通過爬蟲獲取同行業(yè)的數(shù)據(jù)然后進行分析, 進而指導公司的策略指定。
2. 爬蟲的基本步驟
基本步驟:
起始URL地址
發(fā)出請求獲取響應數(shù)據(jù)
對響應數(shù)據(jù)解析
數(shù)據(jù)入庫
3. 安裝requests模塊
- requests : 可以模擬瀏覽器的請求
- 官方文檔 :http://cn.python-requests.org/zh_CN/latest/
- 安裝 :pip install requests
快速入門(requests三步走):
# 導入模塊
import requests
# 通過requests.get()發(fā)送請求
# data保存返回的響應數(shù)據(jù)(這里的響應數(shù)據(jù)不是單純的html,需要通過content獲取html代碼)
data = requests.get("http://www.baidu.com")
# 通過data.content獲取html代碼
data = data.content.decode("utf-8")
4. 爬取照片
① 查看index.html

② 爬取照片步驟
- 獲取index.html代碼
- 解析index.html代碼獲取圖片url
- 通過圖片url獲取圖片
③ 獲取index.html代碼
# 通過爬蟲向index.html發(fā)送請求
# requests.get(網(wǎng)址): 向一個網(wǎng)址發(fā)送請求,和在瀏覽器中輸入網(wǎng)址是一樣的
data = requests.get("http://127.0.0.1:8000/index.html")
# content可以把requests.get()獲取的返回值中的html內容獲取到
data = data.content.decode("utf-8")
④ 解析index.html代碼獲取圖片url
# 獲取圖片的請求url
def get_pic_url():
# 通過爬蟲向index.html發(fā)送請求
# requests.get(網(wǎng)址): 向一個網(wǎng)址發(fā)送請求,和在瀏覽器中輸入網(wǎng)址是一樣的
data = requests.get("http://127.0.0.1:8000/index.html")
# content可以把requests.get()獲取的返回值中的html內容獲取到
data = data.content.decode("utf-8")
# html每一行都有"
", 對html進行分割獲得一個列表
data = data.split("
")
# 創(chuàng)建一個列表存儲所有圖片的url地址(也就是圖片網(wǎng)址)
url_list = []
for url in data:
# 通過正則解析出所有的圖片url
result = re.match('.*src="(.*)" width.*', url)
if result is not None:
# 把解析出來的圖片url添加到url_list中
url_list.append(result.group(1))
return url_list
⑤ 通過圖片url獲取圖片
# 把爬取到的圖片保存到本地
def save_pic(url_list):
# 通過num給照片起名字 例如:0.jpg 1.jpg 2.jpg
num = 0
for url in url_list:
# 通過requests.get()獲取每一張圖片
pic = requests.get(f"http://127.0.0.1:8000{url[1:]}")
# 保存每一張圖片
with open(f"./source/spyder/{num}.jpg", "wb") as f:
f.write(pic.content)
num += 1
完整代碼
# 把爬取到的圖片保存到本地
def save_pic(url_list):
# 通過num給照片起名字 例如:0.jpg 1.jpg 2.jpg
num = 0
for url in url_list:
# 通過requests.get()獲取每一張圖片
pic = requests.get(f"http://127.0.0.1:8000{url[1:]}")
# 保存每一張圖片
with open(f"./source/spyder/{num}.jpg", "wb") as f:
f.write(pic.content)
num += 1
四. 使用Python爬取GDP數(shù)據(jù)
1. gdp.html

通過訪問 http://127.0.0.1:8080/gdp.html 可以獲取2020年世界GDP排名. 在這里我們通過和爬取照片一樣的流程步驟獲取GDP數(shù)據(jù)。
2. zip函數(shù)的使用
zip() 函數(shù): 用于將可迭代的對象作為參數(shù),將對象中對應的元素打包成一個個元組,然后返回由這些元組組成的列表.
a = [1, 2, 3] b = [4, 5, 6] c = [4, 5, 6, 7, 8] # 打包為元組的列表 zipped = zip(a, b) # 注意使用的時候需要list轉化 print(list(zipped)) >>> [(1, 4), (2, 5), (3, 6)] # 元素個數(shù)與最短的列表一致 zipped = zip(a, c) # 注意使用的時候需要list轉化 print(list(zipped)) >>> [(1, 4), (2, 5), (3, 6)]
3.爬取GDP數(shù)據(jù)
import requests
import re
# 存儲爬取到的國家的名字
country_list = []
# 存儲爬取到的國家gdp的數(shù)據(jù)
gdp_list = []
# 獲取gdp數(shù)據(jù)
def get_gdp_data():
global country_list
global gdp_list
# 獲取gdp的html數(shù)據(jù)
data = requests.get("http://localhost:8000/gdp.html")
# 對獲取數(shù)據(jù)進行解碼
data = data.content.decode("utf8")
# 對gdp的html數(shù)據(jù)進行按行分割
data_list = data.split("
")
for i in data_list:
# 對html進行解析獲取<國家名字>
country_result = re.match('.*<a href=""><font>(.*)</font></a>', i)
# 匹配成功就存放到列表中
if country_result is not None:
country_list.append(country_result.group(1))
# 對html進行解析獲取<gdp數(shù)據(jù)>
gdp_result = re.match(".*¥(.*)億元", i)
# 匹配成功就存儲到列表中
if gdp_result is not None:
gdp_list.append(gdp_result.group(1))
# 把兩個列表融合成一個列表
gdp_data = list(zip(country_list, gdp_list))
print(gdp_data)
if __name__ == '__main__':
get_gdp_data()
五. 多任務爬蟲實現(xiàn)
1. 為什么用多任務
在我們的案例中, 我們只是爬取了2個非常簡單的頁面, 這兩個頁面的數(shù)據(jù)爬取并不會使用太多的時間, 所以我們也沒有太多的考慮效率問題.
但是在真正的工作環(huán)境中, 我們爬取的數(shù)據(jù)可能非常的多, 如果還是使用單任務實現(xiàn), 這時候就會讓我們爬取數(shù)據(jù)的時間很長, 那么顯然使用多任務可以大大提升我們爬取數(shù)據(jù)的效率
2. 多任務爬取數(shù)據(jù)
實際上實現(xiàn)多任務并不難, 只需要使用多任務就可以了
3. 多任務代碼實現(xiàn)
# 獲取gdp
def get_gdp_data():
pass
# 獲取照片
def get_pic():
pass
if __name__ == '__main__':
p1 = multiprocessing.Process(target=get_pic
p2 = multiprocessing.Process(target=get_gdp_data)
p1.start()
p2.start()
六. 數(shù)據(jù)可視化
1. 什么是數(shù)據(jù)可視化

數(shù)據(jù)可視化:顧名思義就是讓數(shù)據(jù)看的到, 他的作用也很明顯, 讓人們不用再去閱讀枯燥無味的數(shù)據(jù), 一眼看去就可以明白數(shù)據(jù)是什么, 數(shù)據(jù)間的關系是什么, 更好的讓我們通過數(shù)據(jù)發(fā)現(xiàn)潛在的規(guī)律進而進行商業(yè)決策。
2. pyecharts模塊

概況 :
Echarts 是個由百度開源的數(shù)據(jù)可視化,憑借著良好的交互性,精巧的圖表設計,得到了眾多開發(fā)者的認可. 而 Python 是門富有表達力的語言,很適合用于數(shù)據(jù)處理. 當數(shù)據(jù)分析遇上數(shù)據(jù)可視化時pyecharts 誕生了.
特性 :
- 簡潔的API設計,使用如絲滑般流暢,支持鏈式調用
- 囊括了**30+**種常見圖表,應有盡有
- 支持主流Notebook 環(huán)境,Jupyter Notebook 和JupyterLab
- 可輕松集成至Flask, Django等主流Web框架
- 高度靈活的配置項,可輕松搭配出精美的圖表
- 詳細的文檔和示例,幫助開發(fā)者更快的上手項目
- 多達400+地圖文件以及原生的百度地圖,為地理數(shù)據(jù)可視化提供強有力的支持
3. 通過pyecharts模塊創(chuàng)建餅狀圖
導入模塊
# 導入餅圖模塊 from pyecharts.charts import Pie # 導入配置選項模塊 import pyecharts.options as opts
初始化餅狀圖:
Pie()函數(shù): 創(chuàng)建餅圖
opts.InitOpts參數(shù): Pie(init_opts=opts.InitOpts(width=“1400px”, height=“800px”))
init_opts: 指定參數(shù)名
opts.InitOpts: 配置選項
**width=“1400px” height=“800px” *界面的寬度和高度
# 創(chuàng)建餅圖并設置這個界面的長和高 # px:像素單位 pie = Pie(init_opts=opts.InitOpts(width="1400px", height="800px"))
給餅圖添加數(shù)據(jù):
add()函數(shù):
參數(shù)1: 名稱
參數(shù)2: 具體數(shù)據(jù), 數(shù)據(jù)類型為==>[(a,b),(a,b),(a,b)]==>a為數(shù)據(jù)名稱,b為數(shù)據(jù)大小
參數(shù)3: 標簽設置 label_opts=opts.LabelOpts(formatter=‘:wppm3vysvbp%’) 符合百分比的形式
# 給餅圖添加數(shù)據(jù) pie.add( "GDP", data, label_opts=opts.LabelOpts(formatter=':wppm3vysvbp%') )
給餅圖添設置標題:
set_global_opts()函數(shù) :
title_opts=opts.TitleOpts : 設置標題
title=“2020年世界GDP排名”, subtitle=“美元” : 設置主標題和副標題
# 給餅圖設置標題 pie.set_global_opts(title_opts=opts.TitleOpts(title="2020年世界GDP排名", subtitle="美元"))
保存數(shù)據(jù):
# 保存結果 pie.render()
4. 完整代碼
import requests
import re
# 導入餅圖模塊
from pyecharts.charts import Pie
# 導入配置選項模塊
import pyecharts.options as opts
# 存儲爬取到的國家的名字
country_list = []
# 春初爬取到的國家gdp的數(shù)據(jù)
gdp_list = []
def get_gdp_data():
global country_list
global gdp_list
# 獲取gdp的html數(shù)據(jù)
data = requests.get("http://localhost:8000/gdp.html")
# 對獲取數(shù)據(jù)進行解碼
data = data.content.decode("utf8")
# 對gdp的html數(shù)據(jù)進行按行分割
data_list = data.split("
")
for i in data_list:
# 對html進行解析獲取<國家名字>
country_result = re.match('.*<a href=""><font>(.*)</font></a>', i)
# 匹配成功就存放到列表中
if country_result is not None:
country_list.append(country_result.group(1))
# 對html進行解析獲取<gdp數(shù)據(jù)>
gdp_result = re.match(".*¥(.*)億元", i)
# 匹配成功就存儲到列表中
if gdp_result is not None:
gdp_list.append(gdp_result.group(1))
# 創(chuàng)建一個餅狀圖顯示GDP前十的國家
def data_view_pie():
# 獲取前十的過的GDP數(shù)據(jù), 同時讓數(shù)據(jù)符合[(),()...]的形式
data = list(zip(country_list[:10], gdp_list[:10]))
# 創(chuàng)建餅圖
pie = Pie(init_opts=opts.InitOpts(width="1400px", height="800px"))
# 給餅圖添加數(shù)據(jù)
pie.add(
"GDP",
data,
label_opts=opts.LabelOpts(formatter=':wppm3vysvbp%')
)
# 給餅圖設置標題
pie.set_global_opts(title_opts=opts.TitleOpts(title="2020年世界GDP排名", subtitle="美元"))
# 保存結果
pie.render()
if __name__ == '__main__':
# 獲取GDP數(shù)據(jù)
get_gdp_data()
# 生成可視化餅圖
data_view_pie()
5. 小結
可視化
- Pie()函數(shù) : 創(chuàng)建餅圖
- add()函數(shù) : 添加數(shù)據(jù)
- set_global_opts()函數(shù) : 設置標題
- render()函數(shù) : 保存數(shù)據(jù)
七. Logging日志模塊
1. logging日志的介紹
在現(xiàn)實生活中,記錄日志非常重要,比如:銀行轉賬時會有轉賬記錄;飛機飛行過程中,會有個黑盒子(飛行數(shù)據(jù)記錄器)記錄著飛機的飛行過程,那在咱們python程序中想要記錄程序在運行時所產生的日志信息,怎么做呢
可以使用 logging 這個包來完成
記錄程序日志信息的目的是:
- 可以很方便的了解程序的運行情況
- 可以分析用戶的操作行為、喜好等信息
- 方便開發(fā)人員檢查bug
2. logging日志級別介紹
日志等級可以分為5個,從低到高分別是:
- DEBUG
- INFO
- WARNING
- ERROR
- CRITICAL
日志等級說明:
- DEBUG:程序調試bug時使用
- INFO:程序正常運行時使用
- WARNING:程序未按預期運行時使用,但并不是錯誤,如:用戶登錄密碼錯誤
- ERROR:程序出錯誤時使用,如:IO操作失敗
- CRITICAL:特別嚴重的問題,導致程序不能再繼續(xù)運行時使用,如:磁盤空間為空,一般很少使用
- 默認的是WARNING等級,當在WARNING或WARNING之上等級的才記錄日志信息。
- 日志等級從低到高的順序是: DEBUG < INFO < WARNING < ERROR < CRITICAL
3. logging日志的使用
在 logging 包中記錄日志的方式有兩種:
- 輸出到控制臺
- 保存到日志文件
日志信息輸出到控制臺的示例代碼:
import logging
logging.debug('這是一個debug級別的日志信息')
logging.info('這是一個info級別的日志信息')
logging.warning('這是一個warning級別的日志信息')
logging.error('這是一個error級別的日志信息')
logging.critical('這是一個critical級別的日志信息')
運行結果:
WARNING:root:這是一個warning級別的日志信息 ERROR:root:這是一個error級別的日志信息 CRITICAL:root:這是一個critical級別的日志信息
說明:
日志信息只顯示了大于等于WARNING級別的日志,這說明默認的日志級別設置為WARNING
logging日志等級和輸出格式的設置:import logging
設置日志等級和輸出日志格式
logging.basicConfig(level=logging.DEBUG,
format=‘%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s’)logging.debug(‘這是一個debug級別的日志信息’)
logging.info(‘這是一個info級別的日志信息’)
logging.warning(‘這是一個warning級別的日志信息’)
logging.error(‘這是一個error級別的日志信息’)
logging.critical(‘這是一個critical級別的日志信息’)
運行結果:
2019-02-13 20:41:33,080 - hello.py[line:6] - DEBUG: 這是一個debug級別的日志信息 2019-02-13 20:41:33,080 - hello.py[line:7] - INFO: 這是一個info級別的日志信息 2019-02-13 20:41:33,080 - hello.py[line:8] - WARNING: 這是一個warning級別的日志信息 2019-02-13 20:41:33,080 - hello.py[line:9] - ERROR: 這是一個error級別的日志信息 2019-02-13 20:41:33,080 - hello.py[line:10] - CRITICAL: 這是一個critical級別的日志信息
代碼說明:
- level 表示設置的日志等級
- format 表示日志的輸出格式, 參數(shù)說明:
- %(levelname)s: 打印日志級別名稱
- %(filename)s: 打印當前執(zhí)行程序名
- %(lineno)d: 打印日志的當前行號
- %(asctime)s: 打印日志的時間
- %(message)s: 打印日志信息
日志信息保存到日志文件的示例代碼:
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',
filename="log.txt",
filemode="w")
logging.debug('這是一個debug級別的日志信息')
logging.info('這是一個info級別的日志信息')
logging.warning('這是一個warning級別的日志信息')
logging.error('這是一個error級別的日志信息')
logging.critical('這是一個critical級別的日志信息')
運行結果:
4. logging日志在Web項目中應用
使用logging日志示例:
程序入口模塊設置logging日志的設置
導入FastAPI模塊
from fastapi import FastAPI
導入響應報文Response模塊
from fastapi import Response
導入服務器uvicorn模塊
import uvicorn
導入日志模塊
import logging
配置日志
logging.basicConfig(level=logging.INFO,
format=‘%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s’,
filename=“log.txt”,
filemode=“w”)訪問index.html時進行日志輸出,示例代碼:
當請求為 /images/0.jpg 時, path ==> 0.jpg
@app.get(“/images/{path}”)
注意這里的參數(shù)需要設置為 path
path : str ==> 指定path為字符串類型的數(shù)據(jù)
def get_pic(path: str):
# 這里open()的路徑就是 ==> f"source/images/0.jpg"
with open(f"source/images/{path}", “rb”) as f:
data = f.read()
# 打log
logging.info(“訪問了” + path)
# return 返回響應數(shù)據(jù)
# Response(content=data, media_type=“jpg”)
# 參數(shù)1: 響應數(shù)據(jù)
# 參數(shù)2: 數(shù)據(jù)格式
return Response(content=data, media_type=“jpg”)訪問gdp.html時進行日志輸出,示例代碼:
@app.get(“/{path}”)
def get_html(path: str):
with open(f"source/html/{path}") as f:
data = f.read()
# 打log
logging.info(“訪問了” + path)
# return 返回響應數(shù)據(jù)
# Response(content=data, media_type=“text/source”
# 參數(shù)1: 響應數(shù)據(jù)
# 參數(shù)2: 數(shù)據(jù)格式
return Response(content=data, media_type=“text/html”)
logging日志:
通過日志信息我們得知, index.html被訪問了2次, gdp.html被訪問了2次.
說明:
- logging日志配置信息在程序入口模塊設置一次,整個程序都可以生效。
- logging.basicConfig 表示 logging 日志配置操作
5. 小結
- 記錄python程序中日志信息使用 logging 包來完成
- logging日志等級有5個:
- DEBUG
- INFO
- WARNING
- ERROR
- CRITICAL
- 打印(記錄)日志的函數(shù)有5個:
- logging.debug函數(shù), 表示: 打印(記錄)DEBUG級別的日志信息
- logging.info函數(shù), 表示: 打印(記錄)INFO級別的日志信息
- logging.warning函數(shù), 表示: 打印(記錄)WARNING級別的日志信息
- logging.error函數(shù), 表示: 打印(記錄)ERROR級別的日志信息
- logging.critical函數(shù), 表示: 打印(記錄)CRITICAL級別的日志信息
總結
到此這篇關于Python多任務爬蟲實現(xiàn)爬取圖片和GDP數(shù)據(jù)的文章就介紹到這了,更多相關Python多任務爬蟲圖片和GDP數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Keras構建神經(jīng)網(wǎng)絡踩坑(解決model.predict預測值全為0.0的問題)
這篇文章主要介紹了Keras構建神經(jīng)網(wǎng)絡踩坑(解決model.predict預測值全為0.0的問題),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Python+Sklearn實現(xiàn)英文文本關鍵詞提取的完整代碼
在自然語言處理(NLP)的文本分析領域,TF-IDF 是實現(xiàn)關鍵詞提取的經(jīng)典加權算法,能精準篩選出文本中兼具單篇高頻和語料庫低頻的核心詞匯,本文將基于Python+Sklearn,針對英文文本實現(xiàn) TF-IDF 的全流程實戰(zhàn),需要的朋友可以參考下2026-03-03
Python實現(xiàn)批量將圖像png格式轉為npy格式
在進行深度學習處理時,有些的代碼處理的數(shù)據(jù)格式為npy,但是常常有的數(shù)據(jù)格式為png,因此本文就來介紹一下Python如何實現(xiàn)圖像批量png格式轉為npy格式,需要的可以參考下2023-12-12
Python 多繼承中的一個詭異現(xiàn)象 既是 Father又是grandfather
我們知道,在面向對象編程里面,繼承是一個很重要的概念。子類可以使用父類的方法和屬性,接下來小編將用舉例的方式為大家講解Python 多繼承中的一個詭異現(xiàn)象 其即是爸爸又是爺爺?shù)钠孑猬F(xiàn)象,感興趣的小伙伴可以看下面文章具體了解2021-09-09
python GUI庫圖形界面開發(fā)之PyQt5中QMainWindow, QWidget以及QDialog的區(qū)別和選擇
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5中QMainWindow, QWidget以及QDialog的區(qū)別和選擇,需要的朋友可以參考下2020-02-02
在Python的Django框架中實現(xiàn)Hacker News的一些功能
這篇文章主要介紹了在Python的Django框架中實現(xiàn)Hacker News的一些功能,包括投票“頂”評論等功能,需要的朋友可以參考下2015-04-04

