簡體   English   中英

用php操作dom來抓取數據

[英]Manipulate dom with php to scrape data

我目前正在嘗試通過 php 操作dom以從 fb 視頻頁面中提取視圖。 下面的代碼一直工作到不久前。 但是現在它沒有找到包含視圖計數的node 此信息位於 id 為fbPhotoPageMediaInfo的 div 中。 通過 php 操作 dom 以獲取 fb 視頻頁面的觀看次數的最佳方法是什么?

private function _callCurl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; Android 5.0.1; SAMSUNG-SGH-I337 Build/LRX22C; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/42.0.2311.138 Mobile Safari/537.36');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
    curl_setopt($ch, CURLOPT_URL, $url);
    $response = curl_exec($ch);
    $http     = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return array(
        $http,
        $response,
    );
}



function test()
{

    $url     = "https://www.facebook.com/TaylorSwift/videos/10153665021155369/";
    $request = callCurl($url);
    if ($request[0] == 200) {
        $dom = new DOMDocument();
        @$dom->loadHTML($request[1]);
        $elm = $dom->getElementById('fbPhotoPageMediaInfo');
        if (isset($elm->nodeValue)) {
            $views = preg_replace('/[^0-9]/', '', $elm->nodeValue);
        } else {
            $views = null;
        }
    } else {
        echo "Error!";
    }

    return isset($views) ? $views : null;
}

這是我確定的...

  1. 如果你在$request上使用var_dump()你可以看到它給你一個 302 代碼(重定向)而不是 200(好的)。
  2. CURLOPT_FOLLOWLOCATION更改為true或將其完全注釋掉會使錯誤消失,但現在我們得到了與預期不同的頁面。

我運行以下命令以查看我被重定向到的位置:

$htm = file_get_contents("https://www.facebook.com/TaylorSwift/videos/10153665021155369/");
var_dump($htm);

這給了我一個頁面,說我使用的是過時的瀏覽器,需要更新它。 所以顯然 Facebook 不喜歡用戶代理。

我更新如下:

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/44.0.2');

這似乎解決了問題。

我個人更喜歡使用 Simplehtmldom。

與其他高流量網站一樣,FB 也會更新其來源以幫助防止抓取。 您將來可能需要調整您的節點搜索。

<?php
$ua = "Mozilla/5.0 (Windows NT 5.0) AppleWebKit/5321 (KHTML, like Gecko) Chrome/13.0.872.0 Safari/5321"; // must be a valid User Agent
ini_set('user_agent', $ua);

require_once('simplehtmldom/simple_html_dom.php'); // http://simplehtmldom.sourceforge.net/

Function Scrape_FB_Views($url) {

    IF (!filter_var($url, FILTER_VALIDATE_URL) === false) {

        // Create DOM from URL
        $html = file_get_html($url);
        IF ($html) {

            IF (($html->find('span[class=fcg]', 3))) { // 4th instance of span with fcg class
                $text = trim($html->find('span[class=fcg]', 3)->plaintext); // get content of span as plain text
                $result = preg_replace('/[^0-9]/', '', $text); // replace all non-numeric characters
            }ELSE{
                $result = "Node is no longer valid."
            }

        }ELSE{
            $result = "Could not get HTML.";
        }
    }ELSE{
        $result = "URL is invalid.";
    }

    return $result;

}

$url = "https://www.facebook.com/TaylorSwift/videos/10153665021155369/";
echo("<p>".Scrape_FB_Views($url)."</p>");
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM