Php 链接标签提取
我有代码来提取链接,但我也需要链接标签。我需要链接存储在一个数组中,链接标签存储在另一个数组中 例如,如果网站bbc.com有代码Php 链接标签提取,php,curl,Php,Curl,我有代码来提取链接,但我也需要链接标签。我需要链接存储在一个数组中,链接标签存储在另一个数组中 例如,如果网站bbc.com有代码,我需要$linklabel[0]=sports和$link[0]=bbc.com/sports.html 代码如下,但错误发生为致命错误:在第14行的C:\wamp\www\test\d.php中调用未定义的方法DOMXPath::find() <?php $url='http://edition.cnn.com/?fbid=4OofUbASN5k';
,我需要$linklabel[0]=sports
和$link[0]=bbc.com/sports.html
代码如下,但错误发生为致命错误:在第14行的C:\wamp\www\test\d.php中调用未定义的方法DOMXPath::find()
<?php
$url='http://edition.cnn.com/?fbid=4OofUbASN5k';
$var = fread_url($url);// function calling to get the page from curl
$search = array('@<script[^>]*?>.*?</script>@si'); // Strip out javascript
$var = preg_replace($search, "\n", html_entity_decode($var)); // Strip out javascript
$linklabel = array();
$link = array();
$dom = new DOMDocument($var);
@$dom->loadHTML($var);
$xpath = new DOMXPath($dom);// Grab the DOM nodes
foreach($xpath->find('a') as $element)
{
array_push($linklabel, $element->innerText);
print $linklabel;
array_push($link, $element->href);
print $link.'<br>';
}
function fread_url($url)
{
if(function_exists("curl_init")){
$ch = curl_init();
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ".
"Windows NT 5.0)";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$html = curl_exec($ch);
//print $html;//printing the web page.
curl_close($ch);
}
else{
$hfile = fopen($url,"r");
if($hfile){
while(!feof($hfile)){
$html.=fgets($hfile,1024);
}
}
}
return $html;
}
?>
使用
你来对地方了。请删除您的电子邮件,因为这是一个共享的社区资源,而不是您的个人问答机
因此,您应该使用来解析链接。然后它就变得像
$dom = file_get_html('http://www.google.com/');
// get the label of all links. see the docs for searching options
foreach ($dom->find('a') as $links)
{
$link->innerText;
$link->href;
}
删除了通过电子邮件与您联系的请求-为了您自己的利益,以防止拒绝投票。:)@佩卡,谢谢你的编辑,我本来打算自己做的+1因为你真的需要代表。这也是正确的答案:D但是->标签不会给出超链接的文本,它会返回标签属性…这里的$element是什么。我正在获取函数find('a')的错误信息。如果你能解释一下,我的项目的实际意图是通过一个php curl代理服务器实现无鼠标浏览插件特性。我想你已经听说过mozilla插件了。得到链接标签后,我需要给它添加数字。然后,通过一个虚拟键盘(只有数字键盘),我将允许身体受到挑战的人用软开关打开链接。
$dom = file_get_html('http://www.google.com/');
// get the label of all links. see the docs for searching options
foreach ($dom->find('a') as $links)
{
$link->innerText;
$link->href;
}