使用 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. 如果你var_dump() $request你可以看到它给你一个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>");
?>