rrylee
12/3/2016 - 12:38 PM

File download with php, node, python (文件下载)

File download with php, node, python (文件下载)

import requests
from contextlib import closing

url = 'http://img3.imgtn.bdimg.com/it/u=214931719,1608091472&fm=21&gp=0.jpg'
headers = {
	'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
}

with closing(requests.get(url, headers=headers, stream=True)) as response:
	with open('img.jpg', 'wb') as fb:
		for chunk in response.iter_content(128):
			fb.write(chunk)
var http = require('http')
var fs = require('fs')


var url = 'http://img3.imgtn.bdimg.com/it/u=214931719,1608091472&fm=21&gp=0.jpg'
var headers = {
	'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
}

http.get({
	host: 'img3.imgtn.bdimg.com',
	path: '/it/u=214931719,1608091472&fm=21&gp=0.jpg',
	headers: headers
}, (res) => {
	let file = fs.createWriteStream('./img3.jpg')
	res.pipe(file)
	file.on('finish', () => {
		file.close()
	})
}, (err) => {
	console.log(err)
})
<?php 

$url = 'http://img3.imgtn.bdimg.com/it/u=214931719,1608091472&fm=21&gp=0.jpg';
$headers = [
	'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $headers['User-Agent']);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$fileBuffer = fopen('img2.jpg', 'w+');
curl_setopt($ch, CURLOPT_FILE, $fileBuffer);

$response = curl_exec($ch);
curl_close($ch);
fclose($fileBuffer);