1
我想从一个页面中找到所有不嵌套在斜体标签中的锚点。 这是我有什么,和它的作品,但链接不按照正确的顺序处理(按照页面的源代码)PHP XPath查询
@$dom->loadHTML($this->html);
$xpath = new DOMXpath($dom);
$anchorlinks = $xpath->query('//a/@href[not(. = //i//a/@href)]');
任何意见,以我应该如何着手中DIFF在两套xpath查询将不胜感激。
谢谢。
$phil = gettingToPhilosophy("http://en.wikipedia.org/Yarn");
for($i=0; $i<30; $i++)
{
$phil->hop();
$phil->processHTML();
}
<?php
class gettingToPhilosophy
{
public $base_url; //base_url to start with
public $target_url; //url to hop to
public $previous_link; //keep track of last link
public $lookup; //cached array of visited links
public $curl; //curl object to execute
public $html; //html retrieved from curl request
public $conn; //database connection resource
public $hoplimit; //maximum number of hops (23 was the median as per the wikipedia article)
public $hop_num; //the number of hops taken to reach the philosophy page
public $id; //id of current link (Primary Key)
public $child_id; //id of next link
function __construct($base_url)
{
$this->base_url = filter_var($base_url, FILTER_VALIDATE_URL);
//determine if url is valid
if (!($this->base_url))
{
die("<font color='red'>Invalid URL</font>");
}
$this->target_url = parse_url($base_url, PHP_URL_PATH);
$this->previous_link = '';
$this->lookup = array();
$this->curl = curl_init();
// Create a user agent as to not get blocked by wikipedia
$userAgent = 'Googlebot/2.1 (http://www.google.bot.com/bot.html)';
// Initialize curl and following options
//curl_setopt($this->curl, CURLOPT_USERAGENT, $userAgent);
//curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
//curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
//curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,true);
//curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
//$this->conn = pg_connect("dbname=Wesley user=Wesley host=localhost") or die("Can't connect to database".pg_last_error());
$this->hoplimit = 30;
$this->hop_num = 0;
$this->id = 1;
$this->child_id = 0;
}
function __destruct()
{
$this->base_url = null;
$this->target_url = null;
$this->previous_link = null;
$this->curl = null;
$this->lookup = null;
//pg_close($this-conn);
$this->conn = null;
$this->id = null;
$this->child_id = null;
}
function hop()
{
//Error handling for cached results of links
if (isset($this->lookup[$this->target_url]))
{
//printLinks();
die("<font color='red'>Never ending loop: $this->target_url has already been seen</font>");
}
$this->lookup[$this->target_url] = 1; //cache the link
$this->child_id++;
$sql = "insert into Philosophy (base_url, childid, link) values('$this->base_url', $this->child_id, '$this->target_url')";
//pg_execute($conn,$sql);
echo "$sql <br/>";
//append nodeValue to wikipedia url scheme
$this->target_url = "http://en.wikipedia.org".$this->target_url;
// Reset url
$userAgent = 'Googlebot/2.1 (http://www.google.bot.com/bot.html)';
// Initialize curl and following options
curl_setopt($this->curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($this->curl, CURLOPT_FAILONERROR, true);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->curl, CURLOPT_AUTOREFERER, true);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_URL,$this->target_url);
// Get html from the page
$this->html = curl_exec($this->curl);
// Error handling for invalid link
if(!$this->html)
{
//$this->printLinks();
//target_url was invalid or not reachable
die("<font color='red'>$this->target_url is invalid or unreachable - Hopped $this->hop_num times</font>");
}
$this->hop_num++;
}
function processHTML()
{
$dom = new DOMDocument();
@$dom->loadHTML($this->html);
$xpath = new DOMXpath($dom);
$anchorlinks = $xpath->query('//a[not(ancestor::i)]/@href');
//$anchorlinks = $dom->getElementsByTagName('a');
echo "<pre>"; print_r(iterator_to_array($anchorlinks)); echo "</pre>"; exit;
foreach($anchorlinks as $anchorlink)
{
if (!$this->isValid($anchorlink->nodeValue)){ continue; }
$this->previous_link = $this->target_url;
$this->target_url = "$anchorlink->nodeValue";
$flag = true;
break;
/*foreach($anchorlink->attributes as $attribute)
{
//skip erroneous links
if ($attribute->nodeName !== 'href') {continue;}
if (!$this->isValid($attribute->nodeValue)){ continue; }
$this->target_url = "$attribute->nodeValue";
$flag = true;
break;
}*/
}
}
function isValid($link)
{
if ($link === $this->previous_link){ return false; }
//links to ignore
if (strstr($link, '#') || stristr($link, 'Help:') || stristr($link, 'navigation') || stristr($link,'[note')
|| strstr($link, '(') || stristr($link, 'File:') || strstr($link, '.jpg') || strstr($link, '?') || stristr($link, 'http')
|| strstr($link, '//') || stristr($link, 'Portal:') || stristr($link, 'Special:') || stristr($link, 'Wikipedia:')
|| stristr($link, 'Talk:') || stristr($link, 'Category:') || stristr($link, 'Main_Page'))
{
return false;
}
return true;
}
function printLinks()
{
$sql = "select childid, link from philosophy where base_url='$this->base_url'";
if ($result = pg_execute($conn, $sql))
{
while ($row = pg_fetch_assoc($result))
{
echo "{$row['childid']}) {$row['link']} <br/>";
}
}
}
}
?>
谢谢!! http://en.wikipedia.org/wiki/Wikipedia:Getting_to_Philosophy我试图解决这个问题,但不幸的是,你的xpath查询不会以正确的顺序处理链接...任何额外的建议? – Wes 2015-02-09 00:05:16
xpath是否按不同顺序处理节点? – Wes 2015-02-09 00:22:48
@Wes不客气。很可能,您的Xpath引擎仅支持XPath 1.0。在版本1.0中,节点被定义为_sets_(让我也指您[Wiki页面](http://en.wikipedia.org/wiki/Set_%28mathematics%29)),这意味着没有特定的顺序他们。但是,引擎通常会以文档顺序返回结果。您必须显示您的所有PHP代码,并发布您当前获得的结果,否则所有投注都将关闭。 – 2015-02-09 00:25:20