python構(gòu)建web服務(wù)
flask內(nèi)容參考:Flask框架入門教程(非常詳細(xì))
flask安裝與運(yùn)行測試
安裝flask
pip install flask
創(chuàng)建一個(gè)webapp.py文件,內(nèi)容如下
from flask import Flask
# 用當(dāng)前腳本名稱實(shí)例化Flask對象,方便flask從該腳本文件中獲取需要的內(nèi)容
app = Flask(__name__)
#程序?qū)嵗枰烂總€(gè)url請求所對應(yīng)的運(yùn)行代碼是誰。
#所以程序中必須要?jiǎng)?chuàng)建一個(gè)url請求地址到python運(yùn)行函數(shù)的一個(gè)映射。
#處理url和視圖函數(shù)之間的關(guān)系的程序就是"路由",在Flask中,路由是通過@app.route裝飾器(以@開頭)來表示的
@app.route("/")
#url映射的函數(shù),要傳參則在上述route(路由)中添加參數(shù)申明
def index():
return "Hello World!"
# 直屬的第一個(gè)作為視圖函數(shù)被綁定,第二個(gè)就是普通函數(shù)
# 路由與視圖函數(shù)需要一一對應(yīng)
# def not():
# return "Not Hello World!"
# 啟動一個(gè)本地開發(fā)服務(wù)器,激活該網(wǎng)頁
app.run()
運(yùn)行代碼
python webapp.py
終端輸出如下:
& D:/ProgramData/Anaconda3/envs/py10/python.exe d:/zjdemo/webapp.py
* Serving Flask app 'webapp'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
127.0.0.1 - - [20/Nov/2023 08:20:47] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [20/Nov/2023 08:20:47] "GET /favicon.ico HTTP/1.1" 404 -
在瀏覽器輸入
http://127.0.0.1:5000
返回如下
flask返回復(fù)雜的html字符串
創(chuàng)建webapp_html_str.py文件,代碼如下:
from flask import Flask
# 用當(dāng)前腳本名稱實(shí)例化Flask對象,方便flask從該腳本文件中獲取需要的內(nèi)容
app = Flask(__name__)
html_str="""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<table id="g570b4" border="1">
<tr id="g419fe">
<th id="g16b02">th標(biāo)頭
</th>
<th id="gaae0b">th標(biāo)頭
</th>
<th id="gd78bc" class=" u5899e">地址
</th>
</tr>
<tr id="g5af9b">
<td id="g920bb">td表格單元
</td>
<td id="g9de93" class=" uab6e6">td表格單元
</td>
<td id="gea8dc">上海浦東虹橋某某小區(qū)某某地點(diǎn)
</td>
</tr>
<tr id="cf47d6" class=" u0cbcd ">
<td id="c913e3" class=" ud690a ">td表格單元
</td>
<td id="c452e0" class=" uab6e6 ">td表格單元
</td>
<td id="c917b3" class=" u7eb06 ">td表格單元
</td>
</tr>
<tr id="cba81f" class=" u0cbcd ">
<td id="c3dae7" class=" ud690a ">td表格單元
</td>
<td id="c7d0f9" class=" uab6e6 ">td表格單元
</td>
<td id="c9fe10" class=" u7eb06 ">td表格單元
</td>
</tr>
</table>
<style>
.u5899e {
width: 162px;
}
</style>
</body>
</html>
"""
#程序?qū)嵗枰烂總€(gè)url請求所對應(yīng)的運(yùn)行代碼是誰。
#所以程序中必須要?jiǎng)?chuàng)建一個(gè)url請求地址到python運(yùn)行函數(shù)的一個(gè)映射。
#處理url和視圖函數(shù)之間的關(guān)系的程序就是"路由",在Flask中,路由是通過@app.route裝飾器(以@開頭)來表示的
@app.route("/")
#url映射的函數(shù),要傳參則在上述route(路由)中添加參數(shù)申明
def index():
return html_str
# 直屬的第一個(gè)作為視圖函數(shù)被綁定,第二個(gè)就是普通函數(shù)
# 路由與視圖函數(shù)需要一一對應(yīng)
# def not():
# return "Not Hello World!"
# 啟動一個(gè)本地開發(fā)服務(wù)器,激活該網(wǎng)頁
app.run()
運(yùn)行
運(yùn)行代碼
python webapp.py
在瀏覽器輸入
http://127.0.0.1:5000
返回如下
flask返回html頁面
返回一個(gè)靜態(tài)html頁面
在工程目錄下,創(chuàng)建一個(gè)templates目錄,在templates目錄創(chuàng)建a.html文件,代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<table id="g570b4" border="1">
<tr id="g419fe">
<th id="g16b02">th標(biāo)頭
</th>
<th id="gaae0b">th標(biāo)頭
</th>
<th id="gd78bc" class=" u5899e">地址
</th>
</tr>
<tr id="g5af9b">
<td id="g920bb">td表格單元
</td>
<td id="g9de93" class=" uab6e6">td表格單元
</td>
<td id="gea8dc">上海浦東虹橋某某小區(qū)某某地點(diǎn)
</td>
</tr>
<tr id="cf47d6" class=" u0cbcd ">
<td id="c913e3" class=" ud690a ">td表格單元
</td>
<td id="c452e0" class=" uab6e6 ">td表格單元
</td>
<td id="c917b3" class=" u7eb06 ">td表格單元
</td>
</tr>
<tr id="cba81f" class=" u0cbcd ">
<td id="c3dae7" class=" ud690a ">td表格單元
</td>
<td id="c7d0f9" class=" uab6e6 ">td表格單元
</td>
<td id="c9fe10" class=" u7eb06 ">td表格單元
</td>
</tr>
</table>
<style>
.u5899e {
width: 162px;
}
</style>
</body>
</html>
此時(shí)項(xiàng)目結(jié)構(gòu)如下:
創(chuàng)建webapp_html.py文件,代碼如下:
from flask import Flask, render_template
app = Flask(__name__)
# “show”與函數(shù)index對應(yīng)
# 運(yùn)行index函數(shù)返回templates目錄下的index.html頁面
@app.route("/show")
def index():
return render_template("a.html")
if __name__ == '__main__':
app.run()
運(yùn)行代碼
python webapp_html.py
輸出如下:
(py10) PS D:\zjdemo> & D:/ProgramData/Anaconda3/envs/py10/python.exe d:/zjdemo/webapp_html.py
* Serving Flask app 'webapp_html'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
127.0.0.1 - - [20/Nov/2023 08:38:23] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [20/Nov/2023 08:38:28] "GET /show HTTP/1.1" 200 -
瀏覽器輸入:
http://127.0.0.1:5000/show
返回如下:
返回一個(gè)動態(tài)html頁面
在templates目錄下創(chuàng)建一個(gè)jsdemo.html,代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
fieldset,#d1 {
padding: 10px;
width: 300px;
margin: 0 auto;
}
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<fieldset>
<legend>按時(shí)</legend>
輸入表格的行數(shù):<input type="text" id="row" value="3" placeholder="請輸入表格的行數(shù)" required autofocus><br>
輸入表格的列數(shù):<input type="text" id="col" value="5" placeholder="請輸入表格的列數(shù)" required autofocus><br>
<input type="button" id="ok" value="產(chǎn)生表格" onclick="createTable()"/>
</fieldset>
</form>
<div id="d1"></div>
<script type="text/javascript">
function createTable(){
n=1;
var str="<table width='100%' border='1' cellspacing='0' cellpadding='0'><tbody>";
var r1=document.getElementById("row").value;
var c1=document.getElementById("col").value;
for(i=0;i<r1;i++)
{
str=str+"<tr align='center'>";
for(j=0;j<c1;j++)
{
str=str+"<td>"+(n++)+"</td>";
}
str=str+"</tr>";
}
var d1=document.getElementById("d1");
d1.innerHTML=str+"</tbody></table>";
}
createTable()
</script>
</body>
</html>
在webapp_html.py中添加如下代碼
@app.route("/jsdemo")
def jsdemo():
return render_template("jsdemo.html")
重新啟動web服務(wù),運(yùn)行代碼
```python
python webapp_html.py
輸出如下:
(py10) PS D:\zjdemo> & D:/ProgramData/Anaconda3/envs/py10/python.exe d:/zjdemo/webapp_html.py
* Serving Flask app 'webapp_html'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
在瀏覽器中輸入
http://127.0.0.1:5000/jsdemo
返回為:
在瀏覽器中輸入
http://127.0.0.1:5000/show
返回為:
通過requests獲取靜態(tài)和動態(tài)html頁面
創(chuàng)建requestsdemo.py
內(nèi)容如下:
import requests
url_one = "http://127.0.0.1:5000/show"
url_two = "http://127.0.0.1:5000/jsdemo"
res_one = requests.get(url_one)
print(res_one.content.decode('utf-8'))
print("--------------------------")
res_two = requests.get(url_two)
print(res_two.content.decode('utf-8'))
運(yùn)行代碼,
python .\requestsdemo.py
輸出如下
(py10) PS D:\zjdemo> python .\requestsdemo.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<table id="g570b4" border="1">
<tr id="g419fe">
<th id="g16b02">th標(biāo)頭
</th>
<th id="gaae0b">th標(biāo)頭
</th>
<th id="gd78bc" class=" u5899e">地址
</th>
</tr>
<tr id="g5af9b">
<td id="g920bb">td表格單元
</td>
<td id="g9de93" class=" uab6e6">td表格單元
</td>
<td id="gea8dc">上海浦東虹橋某某小區(qū)某某地點(diǎn)
</td>
</tr>
<tr id="cf47d6" class=" u0cbcd ">
<td id="c913e3" class=" ud690a ">td表格單元
</td>
<td id="c452e0" class=" uab6e6 ">td表格單元
</td>
<td id="c917b3" class=" u7eb06 ">td表格單元
</td>
</tr>
<tr id="cba81f" class=" u0cbcd ">
<td id="c3dae7" class=" ud690a ">td表格單元
</td>
<td id="c7d0f9" class=" uab6e6 ">td表格單元
</td>
<td id="c9fe10" class=" u7eb06 ">td表格單元
</td>
</tr>
</table>
<style>
.u5899e {
width: 162px;
}
</style>
</body>
</html>
--------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
fieldset,#d1 {
padding: 10px;
width: 300px;
margin: 0 auto;
}
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<fieldset>
<legend>按時(shí)</legend>
輸入表格的行數(shù):<input type="text" id="row" value="3" placeholder="請輸入表格的行數(shù)" required autofocus><br>
輸入表格的列數(shù):<input type="text" id="col" value="5" placeholder="請輸入表格的列數(shù)" required autofocus><br>
<input type="button" id="ok" value="產(chǎn)生表格" onclick="createTable()"/>
</fieldset>
</form>
<div id="d1"></div>
<script type="text/javascript">
function createTable(){
n=1;
var str="<table width='100%' border='1' cellspacing='0' cellpadding='0'><tbody>";
var r1=document.getElementById("row").value;
var c1=document.getElementById("col").value;
for(i=0;i<r1;i++)
{
str=str+"<tr align='center'>";
for(j=0;j<c1;j++)
{
str=str+"<td>"+(n++)+"</td>";
}
str=str+"</tr>";
}
var d1=document.getElementById("d1");
d1.innerHTML=str+"</tbody></table>";
}
createTable()
</script>
</body>
</html>
可以看見,靜態(tài)頁面的源代碼和瀏覽器渲染后的效果相匹配,但動態(tài)頁面捕獲到的源代碼和瀏覽器渲染后的效果差別較大,無法通過xpath等方法獲取數(shù)據(jù)。
此時(shí)工程的完整目錄如下:
備注:html渲染的過程
說說頁面渲染的過程
瀏覽器渲染流程(精講)文章來源:http://www.zghlxwxcb.cn/news/detail-758249.html
總結(jié)
本文主要描述了flask安裝與返回靜態(tài)頁面和動態(tài)頁面的過程,并通過requests庫分布爬取靜態(tài)/動態(tài)頁面,通過比較可以更清晰的了解頁面動態(tài)渲染的意義,以及引出selenium庫的作用。文章來源地址http://www.zghlxwxcb.cn/news/detail-758249.html
到了這里,關(guān)于Python爬蟲技術(shù)系列-03/4flask結(jié)合requests測試靜態(tài)頁面和動態(tài)頁面抓取的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!