Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Html解析和抓取URL';通过使用DOMdocument来实现_Php_Web Scraping_Domdocument - Fatal编程技术网

Php Html解析和抓取URL';通过使用DOMdocument来实现

Php Html解析和抓取URL';通过使用DOMdocument来实现,php,web-scraping,domdocument,Php,Web Scraping,Domdocument,试图抓取URL。但是我的foreach循环只返回前两个元素的URL。再也没有了 功能: function getSiteContent($url) { $html = cache()->rememberForever($url, function () use ($url) { return file_get_contents($url); }); $parser = new \DOMDocument(); $parser->load

试图抓取URL。但是我的
foreach
循环只返回前两个
元素的URL。再也没有了

功能:

function getSiteContent($url)
{
    $html = cache()->rememberForever($url, function () use ($url) {
        return file_get_contents($url);
    });

    $parser = new \DOMDocument();
    $parser->loadHTML($html);
    return $parser;

}
libxml_use_internal_errors(true);

$url = 'http://www.sumitomo-rd-mansion.jp/kansai/';
$parser = getSiteContent($url);

$allDivs = $parser->getElementsByTagName('div');
foreach ($allDivs as $div) {
   if ($div->getAttribute('id') == 'areaWrap') {
      $innerDivs = $div->getElementsByTagName('div');
      foreach ($innerDivs as $innerDiv) {
         if ($innerDiv->getAttribute('class') == 'areaBox clearfix') {
             $links = $innerDiv->getElementsByTagName('a');
             if ($links->length > 0) {
                 $a = $links->item(0);
                 $linkRef = $a->getAttribute('href');
                 $link [] = $linkRef;
             }
         }
      }
   }
}

var_dump($link); 
array(2) {
  [0]=>
  string(65) "http://www.sumitomo-rd-mansion.jp/kansai/higashi_umeda/index.html"
  [1]=>
  string(60) "http://www.sumitomo-rd-mansion.jp/kansai/osaka745/index.html"
}
代码:

function getSiteContent($url)
{
    $html = cache()->rememberForever($url, function () use ($url) {
        return file_get_contents($url);
    });

    $parser = new \DOMDocument();
    $parser->loadHTML($html);
    return $parser;

}
libxml_use_internal_errors(true);

$url = 'http://www.sumitomo-rd-mansion.jp/kansai/';
$parser = getSiteContent($url);

$allDivs = $parser->getElementsByTagName('div');
foreach ($allDivs as $div) {
   if ($div->getAttribute('id') == 'areaWrap') {
      $innerDivs = $div->getElementsByTagName('div');
      foreach ($innerDivs as $innerDiv) {
         if ($innerDiv->getAttribute('class') == 'areaBox clearfix') {
             $links = $innerDiv->getElementsByTagName('a');
             if ($links->length > 0) {
                 $a = $links->item(0);
                 $linkRef = $a->getAttribute('href');
                 $link [] = $linkRef;
             }
         }
      }
   }
}

var_dump($link); 
array(2) {
  [0]=>
  string(65) "http://www.sumitomo-rd-mansion.jp/kansai/higashi_umeda/index.html"
  [1]=>
  string(60) "http://www.sumitomo-rd-mansion.jp/kansai/osaka745/index.html"
}
结果:

function getSiteContent($url)
{
    $html = cache()->rememberForever($url, function () use ($url) {
        return file_get_contents($url);
    });

    $parser = new \DOMDocument();
    $parser->loadHTML($html);
    return $parser;

}
libxml_use_internal_errors(true);

$url = 'http://www.sumitomo-rd-mansion.jp/kansai/';
$parser = getSiteContent($url);

$allDivs = $parser->getElementsByTagName('div');
foreach ($allDivs as $div) {
   if ($div->getAttribute('id') == 'areaWrap') {
      $innerDivs = $div->getElementsByTagName('div');
      foreach ($innerDivs as $innerDiv) {
         if ($innerDiv->getAttribute('class') == 'areaBox clearfix') {
             $links = $innerDiv->getElementsByTagName('a');
             if ($links->length > 0) {
                 $a = $links->item(0);
                 $linkRef = $a->getAttribute('href');
                 $link [] = $linkRef;
             }
         }
      }
   }
}

var_dump($link); 
array(2) {
  [0]=>
  string(65) "http://www.sumitomo-rd-mansion.jp/kansai/higashi_umeda/index.html"
  [1]=>
  string(60) "http://www.sumitomo-rd-mansion.jp/kansai/osaka745/index.html"
}
使用此代码,我只得到第一个和第二个div
areaBox
。然后停在那里。我的foreach循环错了吗?或者是网站上有一些淫秽的东西
停止擦伤?谢谢你帮助我

您可以使用
simple\u html\u dom
获得所需的结果。我使用这个库是因为它支持css选择器。试试下面的脚本

<?php
include("simple_html_dom.php");

$weblink = "http://www.sumitomo-rd-mansion.jp/kansai/";
function fetch_sumitomo_links($weblink)
{
    $htmldoc   = file_get_html($weblink);
    foreach ($htmldoc->find(".name a") as $a) {
        $links[]          = $a->href . '<br>';
    }
    return $links;
}
$items = fetch_sumitomo_links($weblink);
foreach($items as $itemlinks){
    echo $itemlinks;
}
?>

我知道已经有了一个公认的答案,但我不建议使用这个“简单的html\U dom”库,它已经有10多年的历史了,而且已经很久没有开发了。我建议您坚持使用DomDocument,您可以使用XPath查询来避免所有循环:

<?php
$xpath = new \DOMXPath($parser);
$nodes = $xpath->query("//div[@id='areaWrap']//div[contains(@class, 'areaBox')]//a[1]");
foreach ($nodes as $node) {
    $links[] = $node->getAttribute("href");
}
这将给出预期的输出:

array(7) {
  [0]=>
  string(65) "http://www.sumitomo-rd-mansion.jp/kansai/higashi_umeda/index.html"
  [1]=>
  string(60) "http://www.sumitomo-rd-mansion.jp/kansai/osaka745/index.html"
  [2]=>
  string(60) "http://www.sumitomo-rd-mansion.jp/kansai/kyobashi/index.html"
  [3]=>
  string(59) "http://www.sumitomo-rd-mansion.jp/kansai/tsurumi/index.html"
  [4]=>
  string(62) "http://www.sumitomo-rd-mansion.jp/kansai/kitatanabe/index.html"
  [5]=>
  string(47) "http://sumai.tokyu-land.co.jp/branz/umedanorth/"
  [6]=>
  string(63) "http://www.sumitomo-rd-mansion.jp/kansai/momoyamadai/index.html"
}

你试过清除缓存了吗?也许你的缓存中有一个旧版本的站点。@Remul yep,实际上我没有尝试任何效果。。。仍然只得到了两个,再也没有了…您是否尝试过使用
simple\u html\u dom
因为它可以解析所有的文档?不,我没有,我认为DOMdocument比这更好。最后,我也不知道怎么用:)谢谢你,伙计。这真的很有帮助。