Jump to content

Scraping images


graham23s

Recommended Posts

Hi Guys,

 

I'm using bing to scrape some images a user types in a search box:

 

code:

 

                  $html = file_get_html("http://www.bing.com/images/search?q=$searchImages&go=&form=QBIL&qs=n&sk=&sc=8-6#");
                  foreach ($html->find('[class=md_mu]') as $element) {
                      $im = $element->innertext;
                      print $im . "<br />";
                      $fileExtension = substr(strrchr($im, '.'), 0);
                      $fileExtension = strtolower($fileExtension);
                      $replacedName = str_replace(" ", "-", $searchString);
                      $newFileName = "$replacedName-" . time() . "$fileExtension";
                      $content = file_get_contents($im);
                      $fp = fopen("../imgProducts/img-fs/$newFileName", "w");
                      fwrite($fp, $content);
                      fclose($fp);
                      
                      $q = mysql_query("UPDATE `fcs_products` SET `product_fullsize`='$newFileName' WHERE `product_root_keyword`='$searchString'");
                      
                      // Thumbnail
                      
                      //$productThumb = resize_image($fp, $newFileName);
                      //print $productThumb;
                      
                      // Save to database
                      //$save = mysql_query("INSERT INTO `fcs_harvested_images` (`image_id`,`image_url`,`image_root_keyword`,`image_filename`,`image_date`) VALUES ('','$im','$searchImages','$newFileName',NOW())");
                  }

 

$im = $element->innertext; this returns a different image each time when i print it out, but when i do the update it stores the same image in the database rather than a seperate one, is theree a way i can update the database to use a different image each time rather than the same one?

 

thanks for any help guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/197657-scraping-images/
Share on other sites

Your update is inside the loop with the same "WHERE" criteria everytime the loop runs. So when you use print, you'll see each $im result get printed to the browser, but everytime your SQL runs, the same row (or rows) are getting updated with the new information, and all the old information is lost.

 

In other words, this code below gives you the same result as your current code:

 

foreach ($html->find('[class=md_mu]') as $element) {
                      $im = $element->innertext;
                      print $im . "<br />";
                      $fileExtension = substr(strrchr($im, '.'), 0);
                      $fileExtension = strtolower($fileExtension);
                      $replacedName = str_replace(" ", "-", $searchString);
                      $newFileName = "$replacedName-" . time() . "$fileExtension";
                      $content = file_get_contents($im);
                      $fp = fopen("../imgProducts/img-fs/$newFileName", "w");
                      fwrite($fp, $content);
                      fclose($fp);
}

$q = mysql_query("UPDATE `fcs_products` SET `product_fullsize`='$newFileName' WHERE `product_root_keyword`='$searchString'");

Link to comment
https://forums.phpfreaks.com/topic/197657-scraping-images/#findComment-1037440
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.