简体   繁体   English

如何从完整的网站(不是单个页面)中获取特定元素

[英]How to get particular element from a complete website (Not single page)

Want to single element from a complete website. 想要从完整的网站中选择一个元素。 Searched google for few hours and no results. 在Google搜索了几个小时,没有结果。 Maybe i search the wrong term but i can't seems to find a way to do that. 也许我搜索了错误的术语,但是我似乎找不到解决该问题的方法。

I took the sitemap.xml and got all the links in it with the code below. 我获取了sitemap.xml,并使用下面的代码获取了所有链接。

I want to use this XML Links to get element from all the links together. 我想使用此XML链接从所有链接中获取元素。

<?php  

$urls = array();  

$DomDocument = new DOMDocument();
$DomDocument->preserveWhiteSpace = false;
$DomDocument->load('https://www.ivory.co.il/sitemap.xml');
$DomNodeList = $DomDocument->getElementsByTagName('loc');

foreach($DomNodeList as $url) {
    $urls[] = $url->nodeValue;
}

//display it
echo "<pre>";
print_r($urls);
echo "</pre>";

?>

Need help... 需要帮忙...

Using simplexml_load_file (since it's public available): 使用simplexml_load_file (因为它是公共可用的):

<?php
$url = "https://www.ivory.co.il/sitemap.xml";

$xml = simplexml_load_file($url) or die ("Error: Cannot create object");
$locs = array();

for($i=0; $i<count($xml->url); $i++){
    $locs[$i] = (string) $xml->url[$i]->loc;
}

echo "<pre>";
print_r($locs);

OUTPUT: 输出:

Array
(
    [0] => https://www.ivory.co.il/
    [1] => https://www.ivory.co.il/%D7%97%D7%[...]
    [2] => https://www.ivory.co.il/%D7%98%D7%[...]
    [3] => https://www.ivory.co.il/%D7%9B%D7%[...]
    [4] => https://www.ivory.co.il/%D7%9E%D7%[...]
    [5] => https://www.ivory.co.il/%D7%9E%D7%[...]
    [6] => https://www.ivory.co.il/%D7%9E%D7%[...]
    [7] => https://www.ivory.co.il/%D7%9E%D7%[...]
    [8] => https://www.ivory.co.il/%D7%9E%D7%[...]
    [9] => https://www.ivory.co.il/%D7%9E%D7%[...]
    [10] => https://www.ivory.co.il/%D7%9E%D7%[...]
    [...]
)

Then you can access each URI with curl functions, iterating the array of links and treating each access to fetch data (docs are here , and some tips here as well). 然后,您可以访问每个URI与curl功能,迭代链接的阵列和处理每个访问来获取数据(文档是在这里 ,和一些技巧在这里为好)。

Example: 例:

$curl = curl_init();
curl_setopt_array ($curl, array(
          CURLOPT_URL => $locs[1],
          CURLOPT_RETURNTRANSFER => true)
);
$result = curl_exec($curl);
curl_close ($curl);
echo $result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM