關於python爬取異步ajax數據的一些看法

咱們在利用python進行爬取數據的時候,必定會遇到這樣的狀況,在瀏覽器中打開能開到全部數據,可是利用requests去爬取源碼獲得的倒是沒有數據的頁面框架。javascript

出現這樣狀況,是由於別人網頁使用了ajax異步加載,你的requests獲得的只是頁面框架而已。php

遇到這樣的狀況有幾種方法能夠解決:css

  一、分析(f12)network中的響應,從而得到ajax的請求接口,在經過這些接口去得到數據。html

  二、使用selenium這個網頁自動化測試工具,去得到源碼。由於這個工具是等到頁面加載完成採起獲取的整個頁面的代碼,因此理論上是能夠得到頁面完整數據的。前端

我本身測試過一個頁面,也是獲取了完整數據的。有須要的朋友能夠去本身測試。java

 

下面,咱們針對第二種方法,作一個實驗:本地新建一個json.html前端文件和json.php後端腳本。web服務器咱們使用apache(集成環境xampp)。python

json.phpjquery

<?php

    header('Access-Control-Allow-Origin:*'); //表明容許任何網址請求

    $arr = array(
        'testarr' => array(
            'name' => 'panchao',
            'age' => 18,
            'tel' => '15928838350',
            'addr' => 'test'
        )
    );
    
    echo json_encode($arr);

?>

json.htmlweb

<div id='test'>
test
</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
    function easyAjax(requestUrl){
        $.ajax({
            url: requestUrl,
            type: "GET",
            dataType: "json", 
            success: function(msg){
            var a = "<span>"+msg.testarr.name+"</span>";
            //動態的向頁面中加入html元素
            $("#test").append(a);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
            }
        });

    }
    easyAjax("http://localhost:8080/json/json.php")
</script>

而後咱們分別用python的request和selenium(webdriver.Chrome)來作實驗。ajax

request

import requests
r = requests.get("http://localhost:8080/json/json.html")
r.encoding = 'utf-8'
print(r.text)

selenium(webdriver.Chrome)至於selenium怎麼使用我前面的文章中有提到

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=(r'C:\Users\0923\AppData\Local\Google\Chrome\Application\chromedriver.exe'), options=chrome_options)
base_url = "http://localhost:8080/json/json.html"
driver.get(base_url)
print(driver.page_source)

咱們來看結果:

第一種,利用python request請求的方法獲得的頁面數據爲:

<div id='test'>
test
</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
    function easyAjax(requestUrl){
    $.ajax({

    url: requestUrl,
    type: "GET",
    //async : false,
    dataType: "json", 

    success: function(msg){

    var a = "<span>"+msg.testarr.name+"</span>";

    
    console.log(msg);
    $("#test").append(a);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert(XMLHttpRequest.status);
    alert(XMLHttpRequest.readyState);
    alert(textStatus);
    }
    });

    }
    easyAjax("http://localhost:8080/json/json.php")
</script>

 第二種,利用selenium(webdriver.Chrome)方法獲得的頁面數據爲:

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div id="test">
test
<span>panchao</span></div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
    function easyAjax(requestUrl){
    $.ajax({

    url: requestUrl,
    type: "GET",
    //async : false,
    dataType: "json", 

    success: function(msg){

    var a = "&lt;span&gt;"+msg.testarr.name+"&lt;/span&gt;";

    
    console.log(msg);
    $("#test").append(a);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert(XMLHttpRequest.status);
    alert(XMLHttpRequest.readyState);
    alert(textStatus);
    }
    });

    }
    easyAjax("http://localhost:8080/json/json.php")
</script></body></html>

咱們能夠看到以上兩種結果,最主要的差別就是第二種方法(selenium(webdriver.Chrome))獲得的web代碼中包含了ajax異步加載的數據。

<div id="test">
test
<span>panchao</span></div>

而第一種方法(python request)獲得的web代碼中沒有包含ajax異步加載的數據。

<div id='test'>
test
</div>

根據以上結論,證實利用selenium(webdriver.Chrome)來獲取頁面數據,是能夠獲取到javascript腳本加載的數據的。

不知道你們有沒有注意到利用selenium(webdriver.Chrome)來獲取頁面數據的方法還自動的給咱們不全了html的標籤

 

但願能夠幫助到有須要的人。

相關文章
相關標籤/搜索