taz*_*taz 7 php xpath web-crawler hyperlink
我想抓取整个网站,我已经阅读了几个线程,但我无法在第二级获取数据.
也就是说,我可以从起始页面返回链接,但后来我找不到解析链接并获取每个链接内容的方法......
我使用的代码是:
<?php
// SELECT STARTING PAGE
$url = 'http://mydomain.com/';
$html= file_get_contents($url);
// GET ALL THE LINKS OF EACH PAGE
// create a dom object
$dom = new DOMDocument();
@$dom->loadHTML($html);
// run xpath for the dom
$xPath = new DOMXPath($dom);
// get links from starting page
$elements = $xPath->query("//a/@href");
foreach ($elements as $e) {
echo $e->nodeValue. "<br />";
}
// Parse each page using the extracted links?
?>
Run Code Online (Sandbox Code Playgroud)
有人可以通过一个例子来帮我解决最后一部分吗?
我将非常感激!
那么,谢谢你的答案!我尝试了一些东西,但我还没有得到任何结果 - 我是编程的新手..
下面,您可以找到我的两个尝试 - 第一个尝试解析链接,第二个尝试用Curl替换file_get内容:
1)
<?php
// GET STARTING PAGE
$url = 'http://www.capoeira.com.gr/';
$html= file_get_contents($url);
//GET ALL THE LINKS FROM STARTING PAGE
// create a dom object
$dom = new DOMDocument();
@$dom->loadHTML($html);
// run xpath for the dom
$xPath = new DOMXPath($dom);
// get specific elements from the sites
$elements = $xPath->query("//a/@href");
//PARSE EACH LINK
foreach($elements as $e) {
$URLS= file_get_contents($e);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xPath = new DOMXPath($dom);
$output = $xPath->query("//div[@class='content-entry clearfix']");
echo $output ->nodeValue;
}
?>
Run Code Online (Sandbox Code Playgroud)
对于上面的代码,我得到警告:file_get_contents()期望参数1是字符串,第26行的../example.php中给出的对象
2)
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, "http://capoeira.com.gr");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$content= curl_exec($curl);
curl_close($curl);
$dom = new DOMDocument();
@$dom->loadHTML($content);
$xPath = new DOMXPath($dom);
$elements = $xPath->query("//a/@href");
foreach ($elements as $e) {
echo $e->nodeValue. "<br />";
}
?>
Run Code Online (Sandbox Code Playgroud)
我没有结果.我试图回应$ content然后我得到:
您无权访问此服务器上的/.
此外,尝试使用ErrorDocument处理请求时遇到413 Request Entity Too Large错误...
有什么想法吗?:)
请检查下面的代码,希望对您有帮助。
<?php
$html = new DOMDocument();
@$html->loadHtmlFile('http://www.yourdomain.com');
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "//div[@class='A-CLASS-Name']/h3/a/@href" );
foreach ($nodelist as $n){
echo $n->nodeValue."\n<br>";
}
?>
Run Code Online (Sandbox Code Playgroud)
谢谢,罗杰
| 归档时间: |
|
| 查看次数: |
34182 次 |
| 最近记录: |