Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-03 05:23:54 10月03日 Update

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 = "

Tags: 1001 Night attributes regularities expressions tags frames third parties pages tools situations methods movies results websites input between garbled code parameters paradise Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology Microsoft Xiaomi MariaDB