douchuanghan1344
2017-04-24 22:39 阅读 15
已采纳

使用php从url中抓取图像

i am trying to make a page that allows me to grab and save images from another link , so here's what i want to add on my page:

  1. text box (to enter url that i want to get images from).
  2. save dialog box to specify the path to save images.

but what i am trying to do here i want to save images only from that url and from inside specific element.

for example on my code i say go to example.com and from inside of element class="images" grab all images.

notes: not all images from the page, just from inside the element whether element has 3 images in it or 50 or 100 i don't care.

here's what i tried and worked using php

<?php
$html = file_get_contents('http://www.tgo-tv.net');
preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i',$html, $matches ); 
echo $matches[ 1 ][ 0 ];
?>

this gets image name and path but what i am trying to make is a save dialog box and the code must save image directly into that path instead of echo it out

hope you understand

Edit 2

it's ok of Not having save dialog box. i must specify save path from the code

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongzaliang4492 dongzaliang4492 2017-04-24 22:58

    If you want something generic, you can use:

    <?php
        $the_site = "http://somesite.com";
        $the_tag = "div"; #
        $the_class = "images";
    
        $html = file_get_contents($the_site);
        libxml_use_internal_errors(true);
        $dom = new DOMDocument();
        $dom->loadHTML($html);
        $xpath = new DOMXPath($dom);
    
        foreach ($xpath->query('//'.$the_tag.'[contains(@class,"'.$the_class.'")]/img') as $item) {
    
            $img_src =  $item->getAttribute('src');
            print $img_src."
    ";
    
        }
    

    Usage:

    Change the site, tag, which can be a div, span, a, etc. also change the class name.

    For example, change the values to:

    $the_site = "https://stackoverflow.com/questions/23674744/what-is-the-equivalent-of-python-any-and-all-functions-in-javascript";
    $the_tag = "div"; #
    $the_class = "gravatar-wrapper-32";
    

    Output:

    https://www.gravatar.com/avatar/67d8ca039ee1ffd5c6db0d29aeb4b168?s=32&d=identicon&r=PG
    https://www.gravatar.com/avatar/24da669dda96b6f17a802bdb7f6d429f?s=32&d=identicon&r=PG
    https://www.gravatar.com/avatar/24780fb6df85a943c7aea0402c843737?s=32&d=identicon&r=PG
    
    点赞 评论 复制链接分享
  • douza6300 douza6300 2017-04-24 23:33

    Maybe you should try HTML DOM Parser for PHP. I've found this tool recently and to be honest it works pretty well. It was JQuery-like selectors as you can see on the site. I suggest you to take a look and try something like:

    <?php
    require_once("./simple_html_dom.php");
    foreach ($html->find("<tag>") as $<tag>) //Start from the root (<html></html>) find the the parent tag you want to search in instead of <tag> (e.g "div" if you want to search in all divs)
    {
        foreach ($<tag>->find("img") as $img) //Start searching for img tag in all (divs) you found
        {
            echo $img->src . "<br>"; //Output the information from the img's src attribute (if the found tag is <img src="www.example.com/cat.png"> you will get www.example.com/cat.png as result)
        }
    }
    ?>
    

    I hope i helped you less or more.

    点赞 评论 复制链接分享

相关推荐