【问题标题】:Scraping & Returning JSON data php [closed]抓取和返回 JSON 数据 php [关闭]
【发布时间】:2013-11-15 22:46:35
【问题描述】:

我目前有一个脚本,可以在这里找到: http://ddelay.co.uk/bus/find_services2.php

使用以下代码:

<?php
include('simple_html_dom.php');
$service = $_GET["stop"];
$debug = $_GET["debug"];

$url = $service;
if($debug === "yes"){
echo "URL IS: " . $url . "\n";
}

$search = array('?', ' ', '.asp&');
$replace = array('&', '+', '.asp?');

$url2 = str_replace($search, $replace, $url);
if($debug === "yes"){
echo "REPLACEMENTS: ". $url2 . "\n";
}
$end = "http://tsy.acislive.com" . $url2 . '&showall=1';
if($debug === "yes"){
echo "FINAL URL: ". $end;
}

$html = file_get_html($end);

$ret = $html-> getElementsByTagName('table');

print($ret);
?>

例如,将从 tsy.acislive.com 拉表(例如:http://ddelay.co.uk/bus/find_services2.php?stop=/web/public_service_stops.asp?service=22?operatorid=36?systemid=30?goingto=Woodhouse

然后我希望能够将此表转换为 JSON 数据以在我的应用程序中使用。不幸的是,我尝试过 PHP 的函数 JSON_encode($ret);但不幸的是,失败了。有人知道我如何将使用 Simple-Dom-Parser php 提取的这个表转换为 Json 数据

【问题讨论】:

  • Warning: file_get_contents() [function.file-get-contents]: Couldn't resolve host name in simple_html_dom.php on line 76

标签: php json web-scraping simpledom


【解决方案1】:

如果你想转换成 JSON,json_encode你应该这样做的方式。这是一个非常易于使用的功能。如果您遇到问题,那将是一个潜在的原因。

试试这些,看看你的数据是什么样子的:

var_dump($html);
var_dump($ret);

From the PHP manual,传递给json_encodeCan be any type except a resource.的值,所以如果它失败了,我只能假设你没有传递正确的数据。

发布那些var_dump 调用的结果,我会为您编辑它并为您提供解决方案。

【讨论】:

  • 不幸的是,转储太大而无法发布,是否有缩小它们的方法?谢谢。
  • 摘自顶部
  • 谢谢,摘录如下: Ret var_dump:pastebin.com/9a5mST7m HTML var_dump:pastebin.com/pz65ndtG 再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 2011-03-10
  • 2012-02-28
  • 1970-01-01
相关资源
最近更新 更多