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

Get an attribute value of a tag on a html page without using a third-party frame

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In most cases, when we want to get a certain attribute value of a tag in the html code, such as the page source code, it is impossible to bother to use a third-party framework. In this case, we can use simple regular expressions to extract the data we want.

For example, I want the next series, most websites provide us with a lot of lists, we need to click one by one, this is very troublesome, now, we just need to use http to get the html string of the page, and then use regular expressions to get the value of the href attribute of the a tag in batches.

Now let's take the downloading of a certain movie website as an example. Now the demo is as follows:

Tool method httpSendGet:

Public static String httpSendGet (String url, String param,String charsetName) {String result = "; BufferedReader in = null; try {String urlNameString = url +"? "+ param; URL realUrl = new URL (urlNameString); / / Open the connection to URL URLConnection connection = realUrl.openConnection () / / set general request properties connection.setRequestProperty ("accept", "* / *"); connection.setRequestProperty ("connection", "Keep-Alive"); connection.setRequestProperty ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1 Sv1)") / / establish the actual connection connection.connect (); / / define the BufferedReader input stream to read the URL response in = new BufferedReader (new InputStreamReader (connection.getInputStream (), charsetName)); String line; while ((line = in.readLine ())! = null) {result + = line }} catch (Exception e) {System.out.println ("an exception occurred sending a GET request!" + e); e.printStackTrace ();} / / use the finally block to close the input stream finally {try {if (in! = null) {in.close ();}} catch (Exception e2) {e2.printStackTrace () }} return result;}

Tool method match:

Public static List match (String source, String element, String attr) {List result = new ArrayList (); String reg = "

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

Internet Technology

Wechat

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

12
Report