PHP-从类内获取文本

PHP-从类内获取文本,php,Php,我正在尝试使用PHP从网页收集文本,这样当该网站上的文本更新时,它也会自动更新 以网站为例,在类robux text中,有一个数字说R$20003-我的目标是从Roblox获得文本,并将其发送到我的网站 我已尝试使用该代码执行此操作,但无效-我遇到以下错误: 警告:file\u get\u contents():php\u network\u getaddresses:getaddrinfo 失败:中的名称解析暂时失败 /第9行的home/public_html/index.php 警告: 文件

我正在尝试使用PHP从网页收集文本,这样当该网站上的文本更新时,它也会自动更新

以网站为例,在类
robux text
中,有一个数字说R$20003-我的目标是从Roblox获得文本,并将其发送到我的网站

我已尝试使用该代码执行此操作,但无效-我遇到以下错误:

警告:file\u get\u contents():php\u network\u getaddresses:getaddrinfo 失败:中的名称解析暂时失败 /第9行的home/public_html/index.php

警告: 文件获取内容(): 无法打开流:php\u network\u getaddresses:getaddrinfo失败: 上的/home/public_html/index.php中的名称解析暂时失败 第9行

警告:DOMDocument::loadHTML():第11行的/home/public_html/index.php中作为输入提供的空字符串


您的系统(
php.ini
)似乎禁用了
allow\u url\u fopen
),这就是出现错误的原因

尝试以下方法:

loadHTML($html);
$finder=newdomxpath($DOM);
$classname='robux text';
$nodes=$finder->query(“/*[包含(@class,$classname')]”);
foreach($node作为$node){
echo$node->nodeValue;
}
?>

您可以通过curl轻松获取url的html内容。您只需将returntransfer选项设置为true。

您的代码看起来不错,问题似乎与www.roblox.com的域名解析有关。有什么解决方案吗?也许可以通过curl试试,如果您设置returntransfer选项,您应该可以获得该站点的html源代码。谢谢,我一开始收到这段代码的错误,但在添加libxml_use_internal_errors(true);,文本正在显示,没有错误。非常感谢!好!!你终于得到了页面html:)它们只是警告,你可以放心地忽略它们,我已经用
libxml\u-use\u-internal\u错误(true)更新了我的答案
刚刚结束查询,我提供的url上有多个robux文本类-有没有办法让它只抓取第一个robux文本类中的文本?您可以尝试使用
$nodes=$finder->query(“/*[contains(@class,$classname')”)”;echo$nodes->item(0)->nodeValue
不带foreach循环。谢谢你,太棒了-我真的很感谢你的帮助!
<?php
$html = file_get_contents("http://www.roblox.com/CW-Ultimate-Amethyst-Addiction-item?id=188004500");
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$finder = new DomXPath($DOM);
$classname = 'robux-text';
$nodes = $finder->query("//*[contains(@class, '$classname')]");
foreach ($nodes as $node) {
  echo $node->nodeValue;
}
?>
<?php
libxml_use_internal_errors(true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.roblox.com/CW-Ultimate-Amethyst-Addiction-item?id=188004500");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);

$DOM = new DOMDocument();
$DOM->loadHTML($html);
$finder = new DomXPath($DOM);
$classname = 'robux-text';
$nodes = $finder->query("//*[contains(@class, '$classname')]");
foreach ($nodes as $node) {
  echo $node->nodeValue;
}
?>