国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python爬蟲技術(shù)系列-03/4flask結(jié)合requests測試靜態(tài)頁面和動態(tài)頁面抓取

這篇具有很好參考價(jià)值的文章主要介紹了Python爬蟲技術(shù)系列-03/4flask結(jié)合requests測試靜態(tài)頁面和動態(tài)頁面抓取。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

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

返回如下
Python爬蟲技術(shù)系列-03/4flask結(jié)合requests測試靜態(tài)頁面和動態(tài)頁面抓取,爬蟲,python,爬蟲,flask

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

返回如下
Python爬蟲技術(shù)系列-03/4flask結(jié)合requests測試靜態(tài)頁面和動態(tài)頁面抓取,爬蟲,python,爬蟲,flask

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)如下:
Python爬蟲技術(shù)系列-03/4flask結(jié)合requests測試靜態(tài)頁面和動態(tài)頁面抓取,爬蟲,python,爬蟲,flask

創(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

返回如下:
Python爬蟲技術(shù)系列-03/4flask結(jié)合requests測試靜態(tài)頁面和動態(tài)頁面抓取,爬蟲,python,爬蟲,flask

返回一個(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

返回為:
Python爬蟲技術(shù)系列-03/4flask結(jié)合requests測試靜態(tài)頁面和動態(tài)頁面抓取,爬蟲,python,爬蟲,flask
在瀏覽器中輸入

http://127.0.0.1:5000/show

返回為:
Python爬蟲技術(shù)系列-03/4flask結(jié)合requests測試靜態(tài)頁面和動態(tài)頁面抓取,爬蟲,python,爬蟲,flask

通過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í)工程的完整目錄如下:
Python爬蟲技術(shù)系列-03/4flask結(jié)合requests測試靜態(tài)頁面和動態(tài)頁面抓取,爬蟲,python,爬蟲,flask

備注: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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Python爬蟲技術(shù)系列-02HTML解析-xpath與lxml

    Python爬蟲技術(shù)系列-02HTML解析-xpath與lxml

    參考連接: XPath教程 https://www.w3school.com.cn/xpath/index.asp lxml文檔 https://lxml.de/index.html#support-the-project 爬蟲專欄 https://blog.csdn.net/m0_38139250/category_12001010.html XPath的中文名稱為XML路徑語言(XML Path Language),其最初的設(shè)計(jì)是用來搜索 XML 文檔,但也適用于HTML文檔搜索。1996年11月,

    2024年02月07日
    瀏覽(20)
  • Python爬蟲技術(shù)系列-06selenium完成自動化測試V01

    Python爬蟲技術(shù)系列-06selenium完成自動化測試V01

    使用selenium庫完成動點(diǎn)擊下一頁,點(diǎn)擊視頻操作等過程, 如果你非要說這是XX,那我也不過多辯解,畢竟 批評不自由,贊美無意義 。 本案例僅是技術(shù)演示,所以會隱去相關(guān)網(wǎng)址等,讀者可以重點(diǎn)查看這里使用的selenium技術(shù)點(diǎn)即可。另外本版本為V01版本,所以僅僅是可用,很多

    2024年02月05日
    瀏覽(25)
  • 可獄可囚的爬蟲系列課程 11:Requests中的SSL

    可獄可囚的爬蟲系列課程 11:Requests中的SSL

    我們在可獄可囚的爬蟲系列課程 09:通過 API 接口抓取數(shù)據(jù)文章中遺留了一個(gè)問題,就是為什么要添加 verify=True 這個(gè)參數(shù),今天我給大家單獨(dú)解釋一下,這還要從網(wǎng)站的 SSL 證書說起。 SSL 證書是數(shù)字證書的一種,類似于駕駛證、護(hù)照、營業(yè)執(zhí)照等的電子副本。SSL 證書也稱為

    2024年02月01日
    瀏覽(19)
  • 【Python爬蟲】requests庫

    【Python爬蟲】requests庫

    1.requests庫的介紹 ? requests 是 Python 語言編寫,基于 urllib3 ,采用 Apache2 Licensed 開源協(xié)議的HTTP庫。它比 urllib 更加方便,可以節(jié)約我們大量的工作,完全滿足 HTTP 測試需求。是 Python 實(shí)現(xiàn)的簡單易用的 HTTP 庫。 Requests 中文文檔:http://docs.pythonrequests.org/zh_CN/latest/index.html ? 解決

    2024年02月16日
    瀏覽(22)
  • python爬蟲—requests

    python爬蟲—requests

    類型 : models.Response r.text : 獲取網(wǎng)站源碼 r.encoding :訪問或定制編碼方式 r.url :獲取請求的 url r.content :響應(yīng)的字節(jié)類型 r.status_code :響應(yīng)的狀態(tài)碼 r.headers :響應(yīng)的頭信息 找登錄接口 ?找參數(shù)的值 python代碼 登錄超級鷹官網(wǎng):超級鷹驗(yàn)證碼識別-專業(yè)的驗(yàn)證碼云端識別服務(wù)

    2024年02月10日
    瀏覽(19)
  • python-網(wǎng)絡(luò)爬蟲.Request

    python-網(wǎng)絡(luò)爬蟲.Request

    Request python中requests庫使用方法詳解: 一簡介: ????????Requests 是Python語言編寫,基于urllib, ????????采用Apache2 Licensed開源協(xié)議的 HTTP 庫。 ????????與urllib相比,Requests更加方便,處理URL資源特別流暢。 ????????可以節(jié)約我們大量的工作,建議爬蟲使用Requests庫

    2024年02月14日
    瀏覽(27)
  • 【python爬蟲】設(shè)計(jì)自己的爬蟲 1. request封裝

    通過requests.session().request 封裝request方法 考慮到請求HTTP/2.0 同時(shí)封裝httpx 來處理HTTP/2.0的請求 通過is_http2來區(qū)分 測試代碼如下

    2024年02月08日
    瀏覽(24)
  • Python爬蟲之requests模塊

    requests文檔http://docs.python-requests.org/zh_CN/latest/index.html 1、requests模塊的作用: 發(fā)送http請求,獲取響應(yīng)數(shù)據(jù) 2、requests模塊是一個(gè)第三方模塊,需要在你的python(虛擬)環(huán)境中額外安裝 pip/pip3 install requests 3、requests模塊發(fā)送get請求 需求:通過requests向百度首頁發(fā)送請求,獲取該頁面

    2024年02月09日
    瀏覽(18)
  • python爬蟲——request模塊講解,Python詳解

    python爬蟲——request模塊講解,Python詳解

    對于GET方式的請求,瀏覽器會把http header和data一并發(fā)送出去,服務(wù)器響應(yīng)200(返回?cái)?shù)據(jù)); 而對于POST, 瀏覽器先發(fā)送header,服務(wù)器響應(yīng)100 continue,瀏覽器再發(fā)送data,服務(wù)器響應(yīng)200 ok(返回?cái)?shù)據(jù))。 (二)http常見請求參數(shù) url:請求url地址 headers:請求頭 **data:發(fā)送編碼為表

    2024年04月26日
    瀏覽(18)
  • python-requests庫(爬蟲)

    網(wǎng)頁數(shù)據(jù)獲取有python自帶的urllib,也有第三方庫requests requests.request(url) 構(gòu)造一個(gè)請求 requests.get(url,params=None) 發(fā)送get請求,結(jié)果為response對象 requests.post(url,data=None,json=None) 發(fā)送post請求 requests.put() 發(fā)送put請求 requests.head() 獲取html的頭信息 requests.delete() 提交刪除請求 requests.pat

    2024年02月08日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包