Created
December 3, 2016 12:38
-
-
Save sword-jin/f32b42fb008c5a665b8db4c2af48ed7b to your computer and use it in GitHub Desktop.
File download with php, node, python (文件下载)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment