How would I scrape this JSON info using PHP and MySQL?

不羁的心 提交于 2019-12-12 22:42:31

问题


Here's the info I'm trying to break up into a database. I'm going to be using this only for my own use to analyse statistics and all that. I have been manually doing it with Excel but I'd like to save myself some work in future.

URL IS: http://fantasy.premierleague.com/web/api/elements/537/

Any idea how to scrape that info or easily convert it to excel format? I know a bit of php and mysql, but nothing about JSON and very little about scraping (I tried messing with SIMPLE_HTML_DOM).


回答1:


You need to JSON_decode the data in PHP.

$obj = JSON_decode($mydata));
print_r($obj);

Extra information for you: http://php.net/manual/en/function.json-decode.php




回答2:


You can convert it into an array as

 $array = json_decode(file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/'));

json_decode()

You could use PEAR excel writer to convert it into excel




回答3:


PHP has a json parser function json_decode().

So:

  1. Use the file_get_contents() function to read the json content from the URL into a string.

  2. Use json_decode() to create a PHP structure representation.

  3. Use PEAR Spreadsheet_Excel_Writer module to create your excel spreadsheet.

Yeah. Easy as 1, 2, 3.




回答4:


<?php

$x=json_decode(file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/'));
print_r($x);//$x will contain all the values in an array format.

?>



回答5:


<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$json= file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/', false, $context);
$arr= json_decode($json);

$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($arr, array ($xml, 'addChild'));
print $xml->asXML();



回答6:


PHP makes it very easy:

$str = file_get_contents('http://fantasy.premierleague.com/web/api/elements/537/');
$jsonarray = json_decode($str, true);
var_dump($jsonarray);

Of course you will have to analyze the structure of the array and figure out how to decompose it to what you are actually looking for.




回答7:


Try using $obj = json_decode($jsonStr) after you receive your response string by running curl on the URL you mentioned in PHP. Then you can grab params from the json object like

$obj['paramName'];

Then you can do anything you want with the information including putting it into a database.

For simple MySQL interaction in php, check out MySQLConnector class.

http://jakesankey.com/blog/2011/12/php-mysql-helper-class/




回答8:


use just json_decode and get the converted data like this edit

$arr = json_decode('your JSON data',true);

echo $arr['transfers_out'];  // output 490374  //for array
echo $arr->transfers_out;  // output 490374  //for stdClass


来源:https://stackoverflow.com/questions/14097711/how-would-i-scrape-this-json-info-using-php-and-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!