将URL输出的JSON保存到文件中

Ski*_*zit 14 ruby python java bash perl

如何将URL输出的JSON保存到文件中?

例如来自Twitter搜索API(这http://search.twitter.com/search.json?q=hi)

语言并不重要.

编辑//我如何向EOF追加更新?

编辑2 //真的很棒的答案,但我接受了我认为最优雅的那个.

Mat*_*hen 27

这在任何语言中都很容易,但机制各不相同.使用wget和shell:

wget 'http://search.twitter.com/search.json?q=hi' -O hi.json
Run Code Online (Sandbox Code Playgroud)

附加:

wget 'http://search.twitter.com/search.json?q=hi' -O - >> hi.json
Run Code Online (Sandbox Code Playgroud)

使用Python:

urllib.urlretrieve('http://search.twitter.com/search.json?q=hi', 'hi.json')
Run Code Online (Sandbox Code Playgroud)

附加:

hi_web = urllib2.urlopen('http://search.twitter.com/search.json?q=hi');
with open('hi.json', 'ab') as hi_file:
  hi_file.write(hi_web.read())
Run Code Online (Sandbox Code Playgroud)


use*_*396 6

在PHP中:

$outfile= 'result.json';
$url='http://search.twitter.com/search.json?q=hi';
$json = file_get_contents($url);
if($json) { 
    if(file_put_contents($outfile, $json, FILE_APPEND)) {
      echo "Saved JSON fetched from “{$url}” as “{$outfile}”.";
    }
    else {
      echo "Unable to save JSON to “{$outfile}”.";
    }
}
else {
   echo "Unable to fetch JSON from “{$url}”.";
}
Run Code Online (Sandbox Code Playgroud)


Ber*_*rez 5

你可以使用CURL

curl -d "q=hi" http://search.twitter.com -o file1.txt
Run Code Online (Sandbox Code Playgroud)