web sayfasından değer elde

6 Cevap php

Merhaba ben Curl kullanarak okuyorum bir web sitesinin ana sayfası var ve ben sitenize sahip sayfaların sayısını kapmak gerekir.

Bu bilgiler bir div şöyledir: -

<div class="pager">
<span class="page-numbers current">1</span>
<a href="/users?page=2" title="go to page 2"><span class="page-numbers">2</span></a>
<a href="/users?page=3" title="go to page 3"><span class="page-numbers">3</span></a>
<a href="/users?page=4" title="go to page 4"><span class="page-numbers">4</span></a>
<a href="/users?page=5" title="go to page 5"><span class="page-numbers">5</span></a>
<span class="page-numbers dots">&hellip;</span>

<a href="/users?page=15" title="go to page 15"><span class="page-numbers">15</span></a>
<a href="/users?page=2" title="go to page 2"><span class="page-numbers next"> next</span></a>
</div>

Ben ihtiyacım değeri 15 ama bu sitede bağlı olarak herhangi bir sayı olabilir ama her zaman aynı konumda olacaktır.

Nasıl kolayca bu değeri okumak ve PHP bir değişkene atayabilirsiniz.

Teşekkürler

Jonathan

6 Cevap

Sen PHP's DOM module for that. Read the page with DOMDocument::loadhtmlfile(), daha sonra DOMXPath nesne oluşturmak ve class = "sayfa numaraları" niteliği olan belge içinde tüm açıklığı unsurları sorgu kullanabilirsiniz.

(Edit: oops, bu ikinci kod parçacığını görmek, sizin için ne arıyorsanız değil)

$html = '<html><head><title>:::</title></head><body>
<div class="pager">
<span class="page-numbers current">1</span>
<a href="/users?page=2" title="go to page 2"><span class="page-numbers">2</span></a>
<a href="/users?page=3" title="go to page 3"><span class="page-numbers">3</span></a>
<a href="/users?page=4" title="go to page 4"><span class="page-numbers">4</span></a>
<a href="/users?page=5" title="go to page 5"><span class="page-numbers">5</span></a>
<span class="page-numbers dots">&hellip;</span>

<a href="/users?page=15" title="go to page 15"><span class="page-numbers">15</span></a>
<a href="/users?page=2" title="go to page 2"><span class="page-numbers next"> next</span></a>
</div>
</body></html>';

$doc = new DOMDocument;
// since the content "is already here" we use loadhtml(content)
// instead of loadhtmlfile(url) 
$doc->loadhtml($html);
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query('//span[@class="page-numbers"]');
echo 'there are ', $nodelist->length, ' span elements having class="page-numbers"';

edit: bunu yapar

<a href="/users?page=15" title="go to page 15"><span class="page-numbers">15</span></a>

(the second last a element) always point to the last page, i.e. does this link contain the value you're looking for?
Then you can use a XPath expression that selects the second but last a element and from there its child span element.

//div[@class="pager"] <- select each <div> where the attribute class equals "pager"
//div[@class="pager"]/a <- select each <a> that is a direct child of the pager div
//div[@class="pager"]/a[position()=last()-1] <- select the <a> that is second but last
//div[@class="pager"]/a[position()=last()-1]/span <- select the direct child <span> of that second but last <a> element in the pager <div>

(Eğer iyi bir XPath öğretici ;-) almak isteyebilirsiniz)

$doc->loadhtml($html);
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query('//div[@class="pager"]/a[position()=last()-1]/span');
if ( 0 < $nodelist->length ) {
  echo $nodelist->item(0)->nodeValue;
}
else {
  echo 'not found';
}

Doğrudan fonksiyon ya da bunu yapmak için kolay bir yolu yoktur. Bunu yapmak için bir existing HTML parser inşa etmek ya da kullanmanız gerekir.

Düzenli ifade ile ayrıştırmak olabilir. İlk <span class="page-numbers">, ardından son birini seçin Tüm occurense bulabilirsiniz:

// div html code should be in $div_html
preg_match_all('#<span class="page-numbers">(\d+)#', $div_html, $page_numbers);
print_r(end($page_numbers[1])); // prints 15

Bu sizin için bir XPath kullanmak isteyebilirsiniz bir şeydir - bir dom belge nesnesi olarak sayfa yükleme gerektirir:

$domDoc = new DOMDocument();
$domDoc->loadHTMLFile("http://path/to/yourfile.html");
$xp = new DOMXPath($domDoc);
$nodes = $xp->query("//xpath/to/relevant/node");
$value = $nodes[0];

Bir süredir iyi bir xpath yazılı değil, bu yüzden bu bölümü anlamaya bazı okuma yapmak gerekir, ama çok zor olmamalı.

belki

$nodes = $dom->getElementsByTagName("span");
$maxPageNum = 0;
foreach($nodes as $node)
{
    if( $node.class == "page-numbers" && $node.value > $maxPageNum )
    {
        $maxPageNum = $node.value;
    }
}

Ben belki o bir dom düğümün sınıf / iç metin erişmek o kadar kolay değil, PHP bilmiyorum, ama bu bilgi ve burada yalancı çalışması gerektiğini almak için bazı yolu olmalı.

Gerçekten iyi çalıştı - sadece büyük bir yardım için Volkerk için teşekkür etmek istedim. Birkaç küçük değişiklikler yapmak zorunda kaldı ve bu ile sona erdi: -

function getusers($userurl)
{
$sSourceData = file_get_contents($userurl);
$doc = new DOMDocument();
@$doc->loadHTML($sSourceData);

$xpath = new DOMXPath($doc);
$nodelist = $xpath->query('//div[@class="pager"]/a[position()=last()-1]/span');
if ( 0 < $nodelist->length ) {

  $lastpage = $nodelist->item(0)->nodeValue;
  $users = $lastpage * 35;
  $userurl = $userurl.'?page='.$lastpage;

  $sSourceData = file_get_contents($userurl);

$doc = new DOMDocument();
@$doc->loadHTML($sSourceData);
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query('//div[@class="user-details"]');
$users = $users + $nodelist->length;
echo 'there are ', $users , ' users';

}
else {
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query('//div[@class="user-details"]');
echo 'there are ', $nodelist->length, ' users';
}


}