Php 如何获取<;a>;本文</a>;使用cURL标记?

Php 如何获取<;a>;本文</a>;使用cURL标记?,php,parsing,curl,html-parsing,Php,Parsing,Curl,Html Parsing,我用这段代码得到了这个错误“致命错误:调用未定义的方法DOMText::getAttribute()”。我想捕获链接的文本,而不是源代码(我不知道它叫什么)。有人可以向我解释我的错误或告诉我一种不同的方法吗?这是我的密码: <?php $target_url = "SITE I WANT"; $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)'; // make the cURL request to $tar

我用这段代码得到了这个错误“致命错误:调用未定义的方法DOMText::getAttribute()”。我想捕获链接的文本,而不是源代码(我不知道它叫什么)。有人可以向我解释我的错误或告诉我一种不同的方法吗?这是我的密码:

<?php

$target_url = "SITE I WANT";
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';

// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);
if (!$html) {
    echo "<br />cURL error number:" .curl_errno($ch);
    echo "<br />cURL error:" . curl_error($ch);
    exit;
}

// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a/text()");

for ($i = 0; $i < $hrefs->length; $i++) {
    $href = $hrefs->item($i);
    $url = $href->getAttribute('href');
    storeLink($url,$target_url);
    echo "<br />Link stored: $url";
}
$id = "12";
   $query = "DELETE FROM links WHERE id<=$id";
    if(!mysql_query($query))
        echo "DELETE failed: $query<br />" . 
        mysql_error() . "<br /><br />";
        ?>

好了:

$document = new DOMDocument();
$document->loadHTML($html);
$selector = new DOMXPath($document);
$anchors = $selector->query('/html/body//a');

foreach($anchors as $a) { 
    $text = $a->nodeValue;
    $href = $a->getAttribute('href');
    echo($text . ' : ' . $href . '<br />');

}
$document=新的DOMDocument();
$document->loadHTML($html);
$selector=newdomxpath($document);
$archors=$selector->query('/html/body//a');
foreach($a){
$text=$a->nodeValue;
$href=$a->getAttribute('href');
echo($text.':'.$href.
); }
检查
$hrefs
的内容。也许您应该改为使用
/html/body//a
,然后在每个元素上尝试检索其文本。您能提供我将使用的代码吗?一般来说,我对这一切都是新手。看看@Adidi的回复,他/她正在编写我刚才评论的代码。编辑是一个他谢谢你^^