Use Jsoup to parse the img element in html

jsoup is a Java HTML parser that can directly parse a URL address and HTML text content. It provides a very low-effort API for fetching and manipulating data through DOM, CSS, and jQuery-like manipulation methods.

QQ 鎴浘20160214125257.jpg


There is a need for a blogging project. When displaying the blog list, display some pictures in the blog to enhance the user experience;

In this case, there are two solutions. The first is to process in the background, store the parsed images in the collection, and then forward them to the page for traversal display; the other is to send the blog content to the page and use Jquery to process it.

I thought about it. It is better to process it in the background, because the project itself uses cnd acceleration, so processing in the front end will affect the efficiency.

In the back-end processing, you can use Jsoup, which is very convenient;


Jsoup homepage: http://jsoup.org/


maven address:

1
2
3
4
5
< dependency >
         < groupId >org.jsoup</ groupId >
         < artifactId >jsoup</ artifactId >
         < version >1.8.3</ version >
     </ dependency >



Here is the key code:

1
2
3
4
5
6
7
8
9
10
11
12
13
for (Blog blog:blogList){
             List<String> imagesList=blog.getImagesList();
             String blogInfo=blog.getContent();
             Document doc=Jsoup.parse(blogInfo);
             Elements jpgs=doc.select( "img[src$=.jpg]" );  // 查找扩展名是jpg的图片
             for ( int  i= 0 ;i<jpgs.size();i++){
                 Element jpg=jpgs.get(i);
                 imagesList.add(jpg.toString());
                 if (i== 2 ){
                     break ;
                 }
             }
         }


Mainly involves some selectors. Not difficult. You can refer to the official documentation. Or Baidu is easy to get started.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325701453&siteId=291194637