dongshi2141 2017-10-04 21:55
浏览 110
已采纳

使用xpath从网页刮取特定文本

I've searched and tried multiple ways to get this but I'm not sure why it won't find most of the information on the webpage.

Page to scrape: https://m.safeguardproperties.com/

Info needed: Version number for PhotoDirect for Apple (currently 4.4.0)

Xpath to text needed (I think) : /html/body/div[1]/div[2]/div[1]/div[4]/div[3]/a

Attempts:

<?php

$file = "https://m.safeguardproperties.com/";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);

$xpath = new DOMXpath($doc);

$elements = $xpath->query("/html/body/div[1]/div[2]/div[1]/div[4]/div[3]/a");

echo "<PRE>";

if (!is_null($elements)) {
  foreach ($elements as $element) {
      var_dump ($element);
    echo "<br/>[". $element->nodeName. "]";

    $nodes = $element->childNodes;
    foreach ($nodes as $node) {
      echo $node->nodeValue. "
";
    }
  }
}

echo "</PRE>";

?>

Second Attempt:

<?PHP
$file = "https://m.safeguardproperties.com/";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);

echo '<pre>';

  // trying to find all links in document to see if I can see the correct one
  $links = [];
  $arr = $doc->getElementsByTagName("a");

  foreach($arr as $item) { 
    $href =  $item->getAttribute("href");
    $text = trim(preg_replace("/[
]+/", " ", $item->nodeValue));
    $links[] = [
      'href' => $href,
      'text' => $text
    ];
  }

var_dump($links);
echo '</pre>';
?>
  • 写回答

1条回答 默认 最新

  • donglin4636 2017-10-04 22:18
    关注

    For that particular website, the versions are being loaded from JSON data client side, you won't find them in the base document.

    http://m.safeguardproperties.com/js/photodirect.json

    This was located by comparing the original document source to the finished DOM and inspecting the network activity in the developer console.

    $url = 'https://m.safeguardproperties.com/js/photodirect.json';
    $json = file_get_contents( $url );
    $object = json_decode( $json );
    echo $object->ios->version; //4.4.0
    

    Please respect other websites and cache your GET request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决