boris / imgscrape
从远程URL抓取图片,获取最大图片
0.3.4
2015-02-24 17:28 UTC
Requires
- guzzlehttp/guzzle: ~5.0
- monolog/monolog: 1.11.*@dev
Requires (Dev)
- mockery/mockery: 0.9.*@dev
- phpunit/phpunit: 4.7.*@dev
This package is not auto-updated.
Last update: 2024-09-28 16:00:00 UTC
README
#安装
通过composer安装
require: {
"boris/imgscrape": "0.*"
}
#使用
请查看提供的index.php文件,以获取工作示例。
本质上,您通过以下方式初始化抓取器
$scraper = new Boris\ImgScraper\Scraper($client, $logger, $configArray);
在这里,$client对象是Guzzle客户端实例,$logger与抓取器位于同一个命名空间中。
获取任何URL上最大图片的源代码
$scraper->getLargestImageUrl($url);
脚本首先发出一个head请求。如果将'imageLinksOnly'参数设置为true,如果响应不包含'Content-Type'头或该头不是图像类型,则返回null。否则,它只返回相同的URL(此功能对于您有大量URL且只想获取直接图像URL非常有用)。
###Symfony
要在Symfony中使用此组件,请将其注册为服务
parameters:
boris.scraper: ~
boris.logger: ~
guzzle.params:
base_url: http://www.reddit.com
services:
boris.logger:
class: Boris\ImgScrape\Logger
arguments: [%boris.logger%]
boris.imgscrape:
class: Boris\ImgScrape\Scraper
arguments: [@guzzle.client, @boris.logger, %boris.scraper%]
guzzle.client:
class: GuzzleHttp\Client
arguments: [%guzzle.params%]
然后您可以从容器中调用它
$this->container->get('boris.imgscrape');
#参数引用
初始化抓取器和记录器组合时,可以覆盖默认参数集
$config = [
'imageLinksOnly' => false,
'acceptedTypes' => [
'jpeg',
'jpg',
'gif',
'png',
],
'blacklist' => [
'www.reddit.com'
],
'user-agent' => 'Boris-ImgScrape/0.2 (amateur script, contact: my at email dot com)'
];
$configLogger = [
'enabled' => true,
'handlers' => [
[
'dir' => __DIR__ . '/../../../../log/debug.log',
'level' => 'debug'
],
[
'dir' => __DIR__ . '/../../../../log/main.log',
'level' => 'info'
],
]
];
这些可以作为您的%scraper%参数值使用,您只需覆盖您需要的部分。以下是每个参数含义的参考
scraper:
imageLinksOnly: only returns the URL if the supplied URL is for and image
acceptedTypes: accepted image mime types
blacklist: which hostnames to ignore
user-agent: your useragent string
logger:
enabled: whether or not to enable the logger
handlers: an array for each logger handler. Supply the dir and the level of the logger (this component uses Monolog, so you can check the default documentation for levels)
#测试
为了运行测试,您需要将以下依赖项包含到项目中,以便composer安装
require-dev: {
"mockery/mockery": "0.9.*@dev",
"phpunit/phpunit": "4.7.*@dev"
}
要运行测试,请导航到项目的根目录并运行
phpunit --group=BorisImgScrape
#日志
默认情况下,Monolog根据记录器配置中'handlers'参数指定的级别创建一个日志文件。您可以使用DEBUG,但请注意,日志会变得相当大。