【问题标题】:Search specific text with DOM XPath使用 DOM XPath 搜索特定文本
【发布时间】:2014-05-15 07:39:51
【问题描述】:

我一直在尝试使用简单的 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”文字。

【问题讨论】:

    标签: xpath simple-html-dom web-crawler


    【解决方案1】:

    您正在以交互方式查询大量网页。它花费的时间超过了您的服务器被允许用于生成页面的时间。

    您可以从命令行执行此脚本以避免超时,或者您可以尝试配置 PHP 和 WebServer,以便它们为脚本提供更多时间(您可以在 https://serverfault.com/ 上询问如何执行此操作)

    【讨论】:

      【解决方案2】:

      嗯,首先你是在混合简单的 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.
      }
      ?>
      

      但实际上,那个网站有一个搜索表单,为什么不直接向它发送查询并阅读结果呢?

      【讨论】:

        猜你喜欢
        • 2012-12-24
        • 1970-01-01
        • 2010-09-19
        • 2023-03-27
        • 2023-03-17
        • 2022-01-25
        • 2021-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多