Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.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 url检索具有特定类名的文本_Php_Html_Xpath_Domdocument_Domxpath - Fatal编程技术网

从PHP url检索具有特定类名的文本

从PHP url检索具有特定类名的文本,php,html,xpath,domdocument,domxpath,Php,Html,Xpath,Domdocument,Domxpath,如何使用PHP从另一个具有特定类名的页面获取文本属性? 我有一个像这样的URL数组列表 $url_array = array( 'https://www.example.com/item/32', 'https://www.example.com/item/33', 'https://www.example.com/item/34' ); 这真的很难解释,所以我画了一幅不太漂亮的素描 过程: 气泡的第一个列表是$url_数组的项,每个项都包含不同的url 现在我需要一

如何使用PHP从另一个具有特定类名的页面获取文本属性? 我有一个像这样的URL数组列表

$url_array = array(
    'https://www.example.com/item/32',
    'https://www.example.com/item/33',
    'https://www.example.com/item/34'
);
这真的很难解释,所以我画了一幅不太漂亮的素描 过程:

气泡的第一个列表是$url_数组的项,每个项都包含不同的url

现在我需要一个方法来读取URL,并获取其内容。 PHP将返回一个
div
元素,该元素具有
-元素和
href
url,但url每次都不同

现在我想从
元素url获取一个内容。它应该返回一个
标记文本内容,并将
文本类
作为自己的类

如何在PHP代码中实现这种方法? 我试过这个,但不起作用:

$htmlAsString = "index.php";
$doc = new DOMDocument();
$doc->loadHTML($htmlAsString);
$xpath = new DOMXPath($doc);
$nodeList = $xpath->query('//a[@class="class-name"]/@href');
for ($i = 0; $i < $nodeList->length; $i++) {
    $url_price = $nodeList->item($i)->value . "<br/>\n";
    $retrieve_text_begin = explode('<div class="text-property">', 
    $url_price);
    $retrieve_text_end = explode('</div>', $retrieve_text_begin[1]);
    echo $retrieve_text_end[0];
}
$htmlastring=“index.php”;
$doc=新的DOMDocument();
$doc->loadHTML($htmlastring);
$xpath=新的DOMXPath($doc);
$nodeList=$xpath->query('//a[@class=“class name”]/@href');
对于($i=0;$i<$nodeList->length;$i++){
$url\u price=$nodeList->item($i)->value。“
\n”; $retrieve\u text\u begin=分解(“”, $url(价格); $retrieve_text_end=分解(“”,$retrieve_text_begin[1]); echo$retrieve_text_end[0]; }

我知道
$htmlastring=“index.php”
可能是问题所在。

您是否尝试过
$htmlastring=file\u get\u contents($url)?它不工作,因为不知何故它返回了解析错误和UTF-8相关错误。我找到了另一个(更简单的)解决方案,但知道如何解决这个问题仍然很有趣。以后可能需要此功能,请查看
DOMDocument
。您可以指定
$encoding