Java-selenium download Baidu Pictures

Source: Internet
Author: User

Package Download;import Java.io.datainputstream;import Java.io.file;import java.io.fileoutputstream;import Java.io.inputstream;import Java.net.httpurlconnection;import Java.net.url;import Java.util.ArrayList;import Java.util.list;import Org.apache.commons.io.input.readerinputstream;import Org.openqa.selenium.by;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.webelement;import Org.openqa.selenium.chrome.chromedriver;public class Geturl {public static list<string> Geturls (String baseUrl) {list<string> urllist=new arraylist<string> (); System.setproperty ("Webdriver.chrome.driver", "E:\\\\webdriver\\\\chromedriverv2.28.exe"); Webdriver Driver = new Chromedriver ();d river.get (BASEURL);//Get all img tags list<webelement> imglist = Driver.findelements (By.tagname ("img")); System.out.println (Imglist.size ()); try {thread.sleep (10000);} catch (Exception e) {e.printstacktrace ();} Convenience all label try {for (webelement a:imglist) {//system.out.println (A.gettext ()); System.out.println(A.getattribute ("src"));//get img tag data-imgurl data-imgurl//get img Tag data-imgurl attribute value string urlstr = A.getattribute (" src ");/*if (Urlstr.contains (". jpg ")) {Urllist.add (URLSTR);} */}} catch (Exception e) {e.printstacktrace ();} return urllist;} public static void Downloadimg (List<string> urllist) throws Exception{url url=null;int Imagenumber = 0;for (String u rlstring:urllist) {url = new URL (urlstring);D atainputstream dis = new DataInputStream (Url.openstream ()); String imageName = "c:\\users\\0\\pictures\\ illustrator \\photos" + Imagenumber + ". jpg"; FileOutputStream Fos =new FileOutputStream (new File (ImageName)); byte[] buffer = new Byte[1024];int length;while ((length = dis.read (buffer)) >0) {fos.write (buffer, 0, length);} Dis.close (); Fos.close (); imagenumber++;}} public static void Main (string[] args) {list<string> urllist=geturls ("https://image.baidu.com/search/index?tn= baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%b2%e5%bb%ad&fr=ala&ala=1& Alatpl=adress&pos=0&amP;hs=2&xthttps=111111 "); System.out.println (Urllist.size ()); try {downloadimg (urllist);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

  

Java-selenium download Baidu Pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.