簡體   English   中英

我現在使用php crawler提取了頁面網址,我想計算每個頁面上的瀏覽總數(點擊數)。如何使用php?p

[英]i fetched page urls using php crawler now i want to count the total number of views (hits) on each page.how can i do it using php ?p

 $main_url="http://programming.com";
 $str = file_get_contents($main_url);

 // Gets Webpage Title
 if(strlen($str)>0)
 {

      $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
      preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
      $title=$title[1];
 }

 // Gets Webpage Description
 $b =$main_url;
 @$url = parse_url( $b );
 @$tags = get_meta_tags($url['scheme'].'://'.$url['host'] );
 $description=$tags['description'];

 // Gets Webpage Internal Links
 $doc = new DOMDocument; 
 @$doc->loadHTML($str); 

 $items = $doc->getElementsByTagName('a'); 
 foreach($items as $value) 
 { 
      $attrs = $value->attributes; 

      $sec_url[]=$attrs->getNamedItem('href')->nodeValue;

 }

 /*foreach ($sec_url as  $value) {
        print_r($value);

        ?>
    <br>
        <?php

}*/

foreach($sec_url as $value)
{

    $sq2 = "insert into datascience (link,title,description,internal_link) 

                     values('$main_url','$title','$description','$value')";  

    $res= mysqli_query($conn, $sq2);

我已將您用來查找各種詳細信息(標題等)的所有各種方法轉換為在加載的文檔中使用XPath。 這只是使事情保持一致。

我發現的主要目的是必須找到一種一致的方式來獲取細節。 在您使用的頁面中,每個段看起來好像都包裹在<article>標記中。 因此,首先獲取所有這些標簽,然后將其用作基礎,然后尋找各種項目。

然后構建XPath表達式以在每個<article>定位它們,意味着您可以選擇每個項目的所有相關詳細信息。 在XPath中-使用descendant軸( descendant::... )表示您想要上下文節點內的節點(作為最后一個參數傳遞給evaluate() )。

$main_url="http://programming.com";
$str = file_get_contents($main_url);

$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc->loadHTML($str);
$xp = new DOMXPath($doc);

$title = $doc->getElementsByTagName("title")[0]->textContent;
$description = $xp->evaluate("string(//meta[@name='description']/@content)");

echo $title.PHP_EOL;
echo $description.PHP_EOL;

$articles = $doc->getElementsByTagName('article');
$pageArticles = [];
foreach($articles as $article) {
    $articleTitle = $xp->evaluate("string(descendant::span[@title='Views'])", $article);
    $articleViews = $xp->evaluate("string(descendant::h2[@class='title'])", $article);
    $pageArticles[] = ["title" => $articleTitle, "views" => $articleViews];
}

print_r($pageArticles);

這只是給我的輸出...

Tutorials - Programming.com
Tap into the collective intelligence of researchers who are working on the same problems you are - right now.

Array
(
    [0] => Array
        (
            [title] => 1,031
            [views] => HTML Cheat Sheet
        )

    [1] => Array
        (
            [title] => 390
            [views] => Best Java Training Institutes In Noida 
        )

    [2] => Array
        (
            [title] => 329
            [views] => Best Salesforce Training institutes in noida
        )

    [3] => Array
        (
            [title] => 382
            [views] => Top Quality Digital Marketing Training Institutes in Noida    
        )

    [4] => Array
        (
            [title] => 308
            [views] => Make your studies with professional Best Oracle Training Institutes in Noida    
        )

    [5] => Array
        (
            [title] => 374
            [views] => Create a Unique Project with a Best Linux Training Institutes in Noida
        )

    [6] => Array
        (
            [title] => 385
            [views] => Webtrackker Technology Best Dot Net Training Institutes Available To Guide the Students 
        )

    [7] => Array
        (
            [title] => 430
            [views] => Availability of My University Help Offers Great Benefit to Students
        )

    [8] => Array
        (
            [title] => 350
            [views] => Webtrackker Institute of Professional Studies: Hadoop Training Institute in Noida    
        )

    [9] => Array
        (
            [title] => 416
            [views] => The Best Quality Digital Marketing Training Institutes in Noida
        )

)

暫無
暫無

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

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