我使用YQL获取一些html页面,以便从中读取信息。因为今天我收到了返回消息"html table不再受支持。请参阅https://policies.yahoo.com/us/en/yahoo/terms/product-atos/yql/index.htm中的YQL使用条款“。
控制台示例:https://developer.yahoo.com/yql/console/#h=select+*+from+html+where+url%3D%22http%3A%2F%2Fwww.google.de%22
雅虎是否停止了这项服务?有没有人知道雅虎的一种公告?我想知道这是一个简单的bug,还是他们真的停止了这项服务……
所有文档仍然在那里(html抓取):https://developer.yahoo.com/yql/guide/yql-select-xpath.html、https://developer.yahoo.com/yql/
不久前,我在雅虎的一个YQL论坛上发帖,现在这个已经不存在了(至少我找不到它了)。你怎么联系雅虎才能知道这项服务是否真的停止了?
致以最好的问候,hebr3
发布于 2017-06-10 19:21:24
非常感谢你的代码。
它帮助我创建了自己的脚本来阅读我需要的页面。我以前从来没有编写过PHP,但是有了你的代码和互联网的智慧,我可以根据我的需要修改你的脚本。
PHP
<?
header('Access-Control-Allow-Origin: *'); //all
$url = $_GET['url'];
if (substr($url,0,25) != "https://www.xxxx.yy") {
echo "Only https://www.xxxx.yy allowed!";
return;
}
$xpathQuery = $_GET['xpath'];
//need more hard check for security, I made only basic
function check($target_url){
$check = curl_init();
//curl_setopt( $check, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));
//curl_setopt($check, CURLOPT_INTERFACE, "xxx.xxx.xxx.xxx");
curl_setopt($check, CURLOPT_COOKIEJAR, 'cookiemon.txt');
curl_setopt($check, CURLOPT_COOKIEFILE, 'cookiemon.txt');
curl_setopt($check, CURLOPT_TIMEOUT, 40000);
curl_setopt($check, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($check, CURLOPT_URL, $target_url);
curl_setopt($check, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($check, CURLOPT_FOLLOWLOCATION, false);
$tmp = curl_exec ($check);
curl_close ($check);
return $tmp;
}
// get html
$html = check($url);
$dom = new DOMDocument();
@$dom->loadHTML($html);
// apply xpath filter
$xpath = new DOMXPath($dom);
$elements = $xpath->query($xpathQuery);
$temp_dom = new DOMDocument();
foreach($elements as $n) $temp_dom->appendChild($temp_dom->importNode($n,true));
$renderedHtml = $temp_dom->saveHTML();
// return html in json response
// json structure:
// {html: "xxxx"}
$post_data = array(
'html' => $renderedHtml
);
echo json_encode($post_data);
?>
Javascript
$.ajax({
url: "url of service",
dataType: "json",
data: { url: url,
xpath: "//*"
},
type: 'GET',
success: function() {
},
error: function(data) {
}
});
发布于 2017-06-10 02:14:08
看起来雅虎确实从2017年6月8日(根据我的错误日志)停止了对html库的支持。目前还没有任何正式的公告。
幸运的是,有一个YQL社区库可以用来代替官方的html库,只需对您的代码库进行很少的更改。请参阅htmlstring table in the YQL Console。
将YQL查询更改为引用htmltable而不是html,并在REST查询中包含社区环境。例如:
/*/ Old code /*/
var site = "http://www.test.com/foo.html";
var yql = "select * from html where url='" + site + "' AND xpath='//div'";
var resturl = "https://query.yahooapis.com/v1/public/yql?q="
+ encodeURIComponent(yql) + "&format=json";
/*/ New code /*/
var site = "http://www.test.com/foo.html";
var yql = "select * from htmlstring where url='" + site + "' AND xpath='//div'";
var resturl = "https://query.yahooapis.com/v1/public/yql?q="
+ encodeURIComponent(yql) + "&format=json"
+ "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
发布于 2017-06-28 00:05:44
尽管YQL不再支持html表,但我已经意识到,可以进行多次调用,而不是进行一次网络调用并解析出结果。例如,我之前的调用如下所示:
select html from rss where url="http://w1.weather.gov/xml/current_obs/KFLL.rss"
这应该会给我下面的信息
现在我必须使用这两个:
select title from rss where url="http://w1.weather.gov/xml/current_obs/KFLL.rss"
select description from rss where url="http://w1.weather.gov/xml/current_obs/KFLL.rss"
。。才能得到我想要的。我不知道为什么他们会在没有明确列出后备选项的情况下弃用这样的东西,但您应该能够以这种方式获取数据。
https://stackoverflow.com/questions/44431212
复制相似问题