我一直在尝试爬行一个网站的页面,并使用简单的html dom和XPath搜索特定的文本。我已经从网站上获得了所有的链接,并试图爬行这些链接,并在所有页面上搜索文本。我要搜索的文本在html span标记内。
但不显示任何输出。
出什么问题了?
以下是我的代码
<?php
include_once("simple_html_dom.php");
set_time_limit(0);
$path='http://www.barringtonsports.com';
$html = file_get_contents($path);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for($i = 0; $i < $hrefs->length; $i++ ){
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
$nurl = $path.$url;
$html1 = file_get_contents($nurl);
$dom1 = new DOMDocument();
@$dom1->loadHTML($html1);
$xpath1 = new DOMXPath($dom1);
$name = $xpath1->evaluate("//span[contains(.,'Asics Gel Netburner 15 Netball Shoes')]");
if($name)
echo"text found";
}
?> 我只想检查一下在www.barringtonsports.com网站的任何页面上是否存在"Asics Gel Netburner 15 Netball Shoes“的文字。
发布于 2014-05-15 15:58:30
你正在交互式地查询大量的网页。它花费的时间超过了您的服务器用来生成页面的时间。
您可以从命令行执行此脚本以避免超时,或者您可以尝试配置PHP和WebServer,以便它们为脚本提供更多时间(您可以在https://serverfault.com/上询问如何做到这一点)。
发布于 2014-05-17 05:00:14
首先,您要混合使用简单的HTML DOM和DOM文档。只需使用其中的一个。因为它在simple-html-dom标记中,所以从命令行开始:
<?php
require_once("./simple_html_dom.php"); # simplehtmldom.sourceforge.net to use manual
$path="http://www.barringtonsports.com";
$html = file_get_html($path);
foreach ($html->find('a') as $anchor) {
$url = $anchor->href;
echo "Found link to " . $url . "\n";
# now see if the link is relative, absolute, or even on another site...
$checkhtml = file_get_html($url);
# now you can parse that link for stuff too.
}
?>但实际上,该网站有一个搜索表单,为什么不直接向它发送一个查询并读取结果呢?
https://stackoverflow.com/questions/23672039
复制相似问题