V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
gdtv
V2EX  ›  PHP

弱弱问一下, PHP + Selenium+chromedrive 怎样将网页上的图片保存到硬盘?

  •  
  •   gdtv · 2017-10-30 17:45:09 +08:00 · 1868 次点击
    这是一个创建于 2367 天前的主题,其中的信息可能已经有所发展或是发生改变。
    $driver = RemoteWebDriver::create($host,$capabilities);
    $driver->get('http://xxx.com/post1.html');
    $images = $driver->findElements(WebDriverBy::cssSelector(".图片 classname"));
    foreach($images as $image){
    $urls[] = $image->getAttribute('src');
    }

    这样获取到所有图片的$urls,然后怎样将这些图片保存到硬盘呢?
    6 条回复    2017-10-31 00:09:12 +08:00
    wq2016
        1
    wq2016  
       2017-10-30 17:56:18 +08:00
    wget
    aru
        2
    aru  
       2017-10-30 18:04:57 +08:00   ❤️ 1
    图片如果没有做特殊处理,拿到 url 后就可以通过 php curl 扩展将它们下载到本地
    或者看看这个文章
    http://www.software-testing-tutorials-automation.com/2015/01/download-and-save-image-using-selenium.html
    gouchaoer
        3
    gouchaoer  
       2017-10-30 19:11:41 +08:00   ❤️ 1
    有 2 个方法,拿到图片的 url,然后用 guzzle 之类的 httpclient 下载,第二就是用截屏保存图片然后根据图片的区域来人工截图
    mingyun
        4
    mingyun  
       2017-10-30 22:36:27 +08:00   ❤️ 1
    原来 PHP 跟 python 一样也有 Selenium

    简单点 file_put_contents('xxx.jpg',file_get_contents($url));
    gdtv
        5
    gdtv  
    OP
       2017-10-30 22:38:00 +08:00
    @gouchaoer
    @mingyun
    谢谢。你们两位大神说的方法我也考虑过,但是对于要登录的网站,file_put_contents 和 guzzle 之类的 httpclient 需要共享 chromedrive 的 cookie 才行,要去处理 cookie 有点麻烦
    gouchaoer
        6
    gouchaoer  
       2017-10-31 00:09:12 +08:00 via Android
    @gdtv selenium 的 api 可以拿到 cookie 的,你把 cookie 放到 guzzle 里就完了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3277 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:42 · PVG 21:42 · LAX 06:42 · JFK 09:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.