2014-07-23 134 views
0

我试图从一个网站拉跨班级到另一个,但似乎无法使其工作。是否可以使用curl从不同网站获取单个单词?从另一个网站拉

什么是span类的正确语法?我的代码我收到警告:

Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/uptickgather.php on line 25 

我的代码:

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://quotes.wsj.com/UEPS'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
libxml_use_internal_errors(true); 
$html = curl_exec($ch); // the whole document (in string) goes in here 
$dom = new DOMDocument(); 
$dom->loadHTML($html); // load it 
libxml_clear_errors(); 
$xpath = new DOMXpath($dom); 

$class = $xpath->query('//[@class="cr_info info_price price_l"]//'); 
echo $class->nodeValue . ' '; 
?> 

第25行是:

echo $class->nodeValue . ' '; 

到目前为止,我已经试图改变查询的格式工作用@语法。因此,我试图用一个斜杠,但据我了解,双斜杠将显示名称为所有属性

"cr_info info_price price_l" 

我也尝试使用以下命令:

$class = $xpath->query('//[@class="cr_info info_price price_l"]//'); 

不过,我似乎无法使其工作。有没有人在xpath/curl有更多的经验对如何解决这个问题有任何建议?

编辑:当我使用VAR倾倒,它看起来像:

object(DOMNodeList)#3 (1) { ["length"]=> int(0) } 

但我试图重新进入编码,和我完全丧失。谁能帮忙?

+3

那么,是第25行? DOMDocument对象或CURL对象是否导致错误?你到目前为止尝试过哪些调试策略? – WillardSolutions

+0

它应该回显什么?让我知道我会帮你取消这个HTML – meda

+0

它应该回应股票的价格。 – user3517904

回答

0

我建议到2结合下列类来从其他网站拉信息:

从所有的HTML标签,内容或标记属性

拉信息:http://simplehtmldom.sourceforge.net/

好办卷曲,支持POST请求:https://github.com/php-curl-class/php-curl-class

所以,在你的榜样:

//download and include the 2 classes: 
include('path/to/curl.php'); 
include('path/to/simple_html_dom.php'); 
$url = 'http://quotes.wsj.com/UEPS'; 

$curl = new Curl; 
$html = str_get_html($curl->get($url)); //pull all html of a website 
$span = $html->find('span[class="cr_info info_price price_l"]',0)->plaintext; //find span tag that contains the following class, 0 means that it is first element that matches tag span and that class, plaintext means it will remove tags leaving only text within tag  
echo $span; //contents of span, e.g. $ 10.811 USD 
相关问题