Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get website Images by Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces Java how to get pictures of the website, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Premise

Recently, my friend visited some websites, saw good pictures and asked me if there was any way not to save a picture manually!

I said use Jsoupya!

Test the website

Open developer mode (F12) and find the link to the corresponding picture. On the Internet, each picture is a link!

First, create a new Maven project, import the Jsoup environment depends on org.jsoup jsoup 1.11.2 II, code public class JsoupTest {public static void main (String [] args) throws IOException {/ / crawler's website String url= "https://mp.weixin.qq.com/s/caU6d6ebpsLVJaf-7gMjtg"; / / get the document object Document document = Jsoup.parse (new URL (url), 10000) / / crawl the part of the code containing the image Element content = document.getElementById ("js_content"); / / get the img tag code which is a collection Elements imgs = content.getElementsByTag ("img"); / / name the id int id=0 of the image For (Element img: imgs) {/ / get the specific picture String pic = img.attr ("data-src"); URL target = new URL (pic); / / get the connection object URLConnection urlConnection = target.openConnection (); / / get the input stream to read the picture information InputStream inputStream = urlConnection.getInputStream () / / get the output stream output address + file name id++; FileOutputStream fileOutputStream = new FileOutputStream ("E:\\ JsoupPic\\" + id+ ".png"); int len=0; / / set a cache byte [] buffer = new byte [1024 * 1024] / / write the picture to E:\ JsoupPic, input the stream to read the data into the buffer, and assign it to len while ((len=inputStream.read (buffer)) > 0) {/ / Parameter 1: picture data Parameter 2: starting length Parameter 3: ending length fileOutputStream.write (buffer, 0, len) } System.out.println (id+ ".png download completed"); / / close the input and output stream and close fileOutputStream.close (); inputStream.close ();}

Results:

Thank you for reading this article carefully. I hope the article "how to get pictures on the Java website" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report