V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
gdtv
V2EX  ›  PHP

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

  •  
  •   gdtv · Oct 30, 2017 · 2312 views
    This topic created in 3100 days ago, the information mentioned may be changed or developed.
    $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 replies    2017-10-31 00:09:12 +08:00
    wq2016
        1
    wq2016  
       Oct 30, 2017
    wget
    aru
        2
    aru  
       Oct 30, 2017   ❤️ 1
    图片如果没有做特殊处理,拿到 url 后就可以通过 php curl 扩展将它们下载到本地
    或者看看这个文章
    http://www.software-testing-tutorials-automation.com/2015/01/download-and-save-image-using-selenium.html
    gouchaoer
        3
    gouchaoer  
       Oct 30, 2017   ❤️ 1
    有 2 个方法,拿到图片的 url,然后用 guzzle 之类的 httpclient 下载,第二就是用截屏保存图片然后根据图片的区域来人工截图
    mingyun
        4
    mingyun  
       Oct 30, 2017   ❤️ 1
    原来 PHP 跟 python 一样也有 Selenium

    简单点 file_put_contents('xxx.jpg',file_get_contents($url));
    gdtv
        5
    gdtv  
    OP
       Oct 30, 2017
    @gouchaoer
    @mingyun
    谢谢。你们两位大神说的方法我也考虑过,但是对于要登录的网站,file_put_contents 和 guzzle 之类的 httpclient 需要共享 chromedrive 的 cookie 才行,要去处理 cookie 有点麻烦
    gouchaoer
        6
    gouchaoer  
       Oct 31, 2017 via Android
    @gdtv selenium 的 api 可以拿到 cookie 的,你把 cookie 放到 guzzle 里就完了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3910 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 00:10 · PVG 08:10 · LAX 17:10 · JFK 20:10
    ♥ Do have faith in what you're doing.