Java obtains network files and Inserts Database code.
Get Baidu's song name, artist and link !!
Copy codeThe Code is as follows:
Package webTools;
Import java. io. BufferedReader;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. UnsupportedEncodingException;
Import java.net. MalformedURLException;
Import java.net. URL;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Import dbTools. DBTools;
Public class IOTOWeb {
Public String getHtmlContent (String htmlURL ){
URL url = null;
String rowContent = "";
StringBuffer htmlContent = new StringBuffer ();
Try {
Url = new URL (htmlURL );
BufferedReader in = new BufferedReader (new InputStreamReader (url
. OpenStream (), "gb2312 "));
While (rowContent = in. readLine ())! = Null ){
HtmlContent. append (rowContent );
}
In. close ();
} Catch (MalformedURLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return htmlContent. toString ();
}
Public List getLink (String htmlContent ){
ArrayList listLink = new ArrayList ();
String regex = "<td [^>] *> [\ (] * <a [^>] * href = (\" ([^ \ "] *) \ "| \ '([^ \'] *) \ '| ([^ \ s>] *) [^>] *> (. *?) [\)] * [\ S] * </td> ";
Pattern pattern = Pattern. compile (regex, Pattern. DOTALL );
Matcher matcher = pattern. matcher (htmlContent );
While (matcher. find ()){
ListLink. add (matcher. group ());
}
Return listLink;
}
Public List <String> getHref (String htmlContent ){
String regex;
List listtHref = new ArrayList ();
Regex = "href = (\" ([^ \ "] *) \" | \ '([^ \'] *) \ '| ([^ \ s>] *) \ "";
Pattern pa = Pattern. compile (regex, Pattern. DOTALL );
Matcher ma = pa. matcher (htmlContent );
While (ma. find ()){
ListtHref. add (ma. group (). replaceFirst ("href = \" "," "). replace ("\"",
""));
}
Return listtHref;
}
Public List <String> getPerson (String htmlContent ){
String regex;
List list = new ArrayList ();
Regex = "\ (<a [^>] * href = (\" ([^ \ "] *) \" | \ '([^ \'] *) \ '| ([^ \ s>] *) [^>] *> (. *?) \\)";
Pattern pa = Pattern. compile (regex, Pattern. DOTALL );
Matcher ma = pa. matcher (htmlContent );
While (ma. find ()){
List. add (ma. group (). replaceFirst ("href = \" "," "). replace ("\"",""));
}
Return list;
}
Public List <String> getSongName (String htmlContent ){
String regex;
List listPerson = new ArrayList ();
Regex = "<a [^>] * href = (\" ([^ \ "] *) \" | \ '([^ \'] *) \ '| ([^ \ s>] *) [^>] *> (. *?) </A> \ s ";
Pattern pa = Pattern. compile (regex, Pattern. DOTALL );
Matcher ma = pa. matcher (htmlContent );
While (ma. find ()){
ListPerson. add (ma. group ());
}
Return listPerson;
}
Public String getMainContent (String htmlContent ){
String regex = "<table width = \" 100% \ "align = \" center \ "cellpadding = \" 0 \ "cellspacing = \" 0 \ "class = \" list \ "> (. *?) </Table> ";
StringBuffer mainContent = new StringBuffer ();
Pattern pattern = Pattern. compile (regex, Pattern. DOTALL );
Matcher matcher = pattern. matcher (htmlContent );
While (matcher. find ()){
MainContent. append (matcher. group ());
}
Return mainContent. toString ();
}
Public String outTag (final String s ){
Return s. replaceAll ("<. *?> ","");
}
DBTools dbTools = new DBTools ();
Public void getFromBaiduMap3 (String htmlURL) throws Throwable {
HashMap htmlContentMap = new HashMap ();
String htmlContent = getHtmlContent (htmlURL );
String mainContent = getMainContent (htmlContent );
List listLink = getLink (mainContent );
For (int j = 0; j <listLink. size (); j ++ ){
String tdTag = listLink. get (j). toString ();
List songNameList = getSongName (tdTag );
String songName = outTag (songNameList. get (0). toString ());
List personList = getPerson (tdTag );
String songPerson = "";
If (personList. size ()! = 0 ){
For (int n = 0; n <personList. size (); n ++ ){
// System. out. println (personList. get (n). toString ());
SongPerson = outTag (personList. get (n). toString ());
}
} Else {
SongPerson = "NONE ";
}
// System. out. print (songNameList. get (0). toString ());
List hrefList = getHref (songNameList. get (0). toString ());
String songHref = hrefList. get (0). toString ();
System. out. println ();
String SQL = "insert into song (songName, songPerson, songHref) values (?,?,?) ";
ArrayList list_values = new ArrayList ();
List_values.add (songName );
List_values.add (songPerson );
List_values.add (songHref );
DbTools. update (SQL, list_values );
}
}
}
DBTools database link class:
Copy codeThe Code is as follows:
Package dbTools;
Import java. util. ArrayList;
Import java. SQL .*;
Public class DBTools {
Private PreparedStatement preparedStatement;
Private ResultSet resultSet;
Private Connection connection;
Public DBTools (){
Try {
Class. forName ("com. mysql. jdbc. Driver ");
} Catch (ClassNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Try {
Connection = DriverManager. getConnection (
"Jdbc: mysql: // localhost: 3306/TestURL", "root", "zhuyi ");
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Public ArrayList query (String SQL, ArrayList list_values) throws Throwable {
ArrayList listRows = new ArrayList ();
PreparedStatement = connection. prepareStatement (SQL );
For (int I = 0; I <list_values.size (); I ++ ){
PreparedStatement. setObject (I + 1, list_values.get (I ));
}
ResultSet = preparedStatement.exe cuteQuery ();
While (resultSet. next ()){
String [] rowinfo = new String [resultSet. getMetaData ()
. GetColumnCount ()];
For (int I = 0; I <rowinfo. length; I ++ ){
Rowinfo [I] = resultSet. getString (I + 1 );
}
ListRows. add (rowinfo );
}
Return listRows;
}
Public void update (String SQL, ArrayList list_values) throws Throwable {
PreparedStatement = connection. prepareStatement (SQL );
For (int I = 0; I <list_values.size (); I ++ ){
PreparedStatement. setObject (I + 1, list_values.get (I ));
}
PreparedStatement.exe cuteUpdate ();
PreparedStatement. close ();
}
}
Servlet call:
Copy codeThe Code is as follows:
Package controller;
Import java. io. IOException;
Import java. io. PrintWriter;
Import java. util. List;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import webTools. IOTOWeb;
Public class TestURL extends HttpServlet {
/**
* Constructor of the object.
*/
Public TestURL (){
Super ();
}
/**
* Destruction of the servlet. <br>
*/
Public void destroy (){
Super. destroy (); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @ Param request
* The request send by the client to the server
* @ Param response
* The response send by the server to the client
* @ Throws ServletException
* If an error occurred
* @ Throws IOException
* If an error occurred
*/
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Try {
IOTOWeb iotoWeb = new IOTOWeb ();
IotoWeb. getFromBaiduMap3 ("http://list.mp3.baidu.com/topso/mp3topsong.html? Id = 1? Top2 ");
} Catch (Throwable e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals
* Post.
*
* @ Param request
* The request send by the client to the server
* @ Param response
* The response send by the server to the client
* @ Throws ServletException
* If an error occurred
* @ Throws IOException
* If an error occurred
*/
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html ");
PrintWriter out = response. getWriter ();
Out
. Println ("<! Doctype html public \ "-// W3C // dtd html 4.01 Transitional // EN \"> ");
Out. println ("<HTML> ");
Out. println ("<HEAD> <TITLE> A Servlet </TITLE> </HEAD> ");
Out. println ("<BODY> ");
Out. print ("This is ");
Out. print (this. getClass ());
Out. println (", using the POST method ");
Out. println ("</BODY> ");
Out. println ("</HTML> ");
Out. flush ();
Out. close ();
}
/**
* Initialization of the servlet. <br>
*
* @ Throws ServletException
* If an error occurs
*/
Public void init () throws ServletException {
// Put your code here
}
}
Obtain the name of a book on the golden book Website:
Copy codeThe Code is as follows:
Package webTools;
Import java. io. BufferedReader;
Import java. io. InputStreamReader;
Import java.net. URL;
Import java. util. ArrayList;
Import java. util. List;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Import dbTools. DBTools;
Public class GetBook {
Public String getHtmlContent (String htmlURL) throws Throwable {
URL url = null;
String rowContent = "";
StringBuffer htmlContent = new StringBuffer ();
Url = new URL (htmlURL );
BufferedReader in = new BufferedReader (new InputStreamReader (url
. OpenStream (), "gb2312 "));
While (rowContent = in. readLine ())! = Null ){
HtmlContent. append (rowContent );
}
In. close ();
Return htmlContent. toString ();
}
Public String getBookName (String htmlContent ){
String bookName = "";
String regex = "<span class = \" style15 \ "> [^>] * </span> ";
Pattern pattern = Pattern. compile (regex, Pattern. DOTALL );
Matcher matcher = pattern. matcher (htmlContent );
If (matcher. find ()){
BookName = matcher. group ();
}
Return bookName;
}
Public String outTag (final String s ){
Return s. replaceAll ("<. *?> ","");
}
DBTools dbtools = new DBTools ();
Public void getFromJINSHU (String htmlURL) throws Throwable {
String htmlContent = getHtmlContent (htmlURL );
String bookName = outTag (getBookName (htmlContent ));
If (bookName! = Null &&! "". Equals (bookName )){
System. out. println (bookName );
String SQL = "insert into bookinfo (bookName) values (?) ";
ArrayList list_values = new ArrayList ();
List_values.add (bookName );
Dbtools. update (SQL, list_values );
}
}
}
Call Servlet:
Copy codeThe Code is as follows:
Package controller;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import webTools. GetBook;
Public class TestBook extends HttpServlet {
/**
* Constructor of the object.
*/
Public TestBook (){
Super ();
}
/**
* Destruction of the servlet. <br>
*/
Public void destroy (){
Super. destroy (); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @ Param request
* The request send by the client to the server
* @ Param response
* The response send by the server to the client
* @ Throws ServletException
* If an error occurred
* @ Throws IOException
* If an error occurred
*/
Int I = 1;
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
GetBook bookinfo = new GetBook ();
For (; I <10000; I ++ ){
String bookURL = "http://www.golden-book.com/booksinfo/12/" + I
+ ". Html ";
Try {
Bookinfo. getFromJINSHU (bookURL );
} Catch (Throwable e ){
I ++;
DoPost (request, response );
}
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals
* Post.
*
* @ Param request
* The request send by the client to the server
* @ Param response
* The response send by the server to the client
* @ Throws ServletException
* If an error occurred
* @ Throws IOException
* If an error occurred
*/
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
GetBook bookinfo = new GetBook ();
For (; I <10000; I ++ ){
String bookURL = "http://www.golden-book.com/booksinfo/12/" + I
+ ". Html ";
Try {
Bookinfo. getFromJINSHU (bookURL );
} Catch (Throwable e ){
I ++;
DoGet (request, response );
}
}
}
/**
* Initialization of the servlet. <br>
*
* @ Throws ServletException
* If an error occurs
*/
Public void init () throws ServletException {
// Put your code here
}
}
There are many implementation methods for each function. I hope you can share different ideas and methods. You can add QQ412546724. Haha