使用 PHP/Python 下载 url 中的特定文件
Download specific file in url using PHP/Python
我以前在 linux 终端上使用 wget -r
下载具有某些扩展名的文件:
wget -r -A Ext URL
但现在我的讲师让我用 PHP 或 Python 做同样的事情。谁能帮忙?
我猜 urllib 很适合你
import urllib
urllib.urlretrieve (URL, file)
您可以使用 PHP 函数 file_get_contents()
来检索文档的内容。该函数的第一个参数是文件名,它可以是文件的本地路径或 URL.
请参阅 PHP docs
中的示例
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
或者,您可以使用 Requests:Requests 是 Python 唯一的非转基因 HTTP 库,可供人类安全使用。
示例(来自文档):
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}
对于 Python,使用网络爬虫库,例如 scrapy。
它有 classes 在传递类似于您在 wget
命令行中输入的参数时完成所有工作。
您可以使用 scrapy pipelines 过滤不需要的下载,并对下载进行增值,例如添加缩略图。
我以前在 linux 终端上使用 wget -r
下载具有某些扩展名的文件:
wget -r -A Ext URL
但现在我的讲师让我用 PHP 或 Python 做同样的事情。谁能帮忙?
我猜 urllib 很适合你
import urllib
urllib.urlretrieve (URL, file)
您可以使用 PHP 函数 file_get_contents()
来检索文档的内容。该函数的第一个参数是文件名,它可以是文件的本地路径或 URL.
请参阅 PHP docs
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
或者,您可以使用 Requests:Requests 是 Python 唯一的非转基因 HTTP 库,可供人类安全使用。
示例(来自文档):
>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf8'
>>> r.encoding
'utf-8'
>>> r.text
u'{"type":"User"...'
>>> r.json()
{u'private_gists': 419, u'total_private_repos': 77, ...}
对于 Python,使用网络爬虫库,例如 scrapy。
它有 classes 在传递类似于您在 wget
命令行中输入的参数时完成所有工作。
您可以使用 scrapy pipelines 过滤不需要的下载,并对下载进行增值,例如添加缩略图。