On the Internet, I saw web page thieves, and some people sneaked into the weather... The most ridiculous thing is that a person posted an article on Google saying that it is a JS version. Let me open it and see, language = VBScript ....... it has also been cited by many people. When a hacker finds a webpage, It is an article that makes people speechless. What makes people speechless is the so-called Super classic joke thief. I have referenced JavaScript code on many websites and cannot see the JS Code at all. Download the source code, which is even more boring. It is implemented using Js. In the result, all JS files are 0kb, which is an empty file.
Today, I wrote a piece about stealing news. Because it is "Stealing", the program must be based on the Code of the website to be stolen.
Simply put, you can open a webpage and view its source files. Then, I use the Java URL class to get the webpage content and use regular expressions to filter the desired content. I wrote a message about stealing Sina news. There are still many places that are not complete, but the basic functions are implemented. Open the page and click a button to start downloading and filtering. Then, store the news into the database.
Run the following code:
Calljsp. jsp:
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"
Http://www.w3.org/TR/html4/loose.dtd
">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> calljsp </title>
</Head>
<Body>
<Form action = "callservlet">
<Button type = "Submit"> Start </button>
</Form>
</Body>
</Html>
Callservlet. Java:
Package servlet;
Import java. Io. ioexception;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import Bo. getcontent;
Public class callservlet extends httpservlet {
Private Static final long serialversionuid = 1l;
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
System. Out. println ("called ");
Getcontent GC = new getcontent ();
GC. getcontent ("http://news.sina.com.cn
");
GC. savatodb ();
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
This. doget (request, response );
}
}
Entity class:
Package DTO;
Public class new {
Private String title;
Private int ID;
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
Private string content;
Public String gettitle (){
Return title;
}
Public void settitle (String title ){
This. Title = title;
}
Public String getcontent (){
Return content;
}
Public void setcontent (string content ){
This. content = content;
}
}
Logic class (key code ):
Package Bo;
Import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java.net. malformedurlexception;
Import java.net. url;
Import java. util. arraylist;
Import java. util. List;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;
Import Dao. newsdao;
Import DTO. New;
Public class getcontent {
Arraylist <string> newslist;
Pattern newspat;
Pattern mainpat;
List <New> News;
Newsdao = new newsdao ();
Int I = 1;
Public getcontent (){
String newsreg = "<li (. *) <(. *) (http: // news [/.] sina [/.] COM [/.] CN (. *) [/.] shtml) + (. *)>. * </a> </Li
> ";
String mainreg = "<p> (. *) </P> ";
Newslist = new arraylist <string> ();
Newspat = pattern. Compile (newsreg );
Mainpat = pattern. Compile (mainreg );
News = new arraylist <New> ();
}
Public void getcontent (string urlstr ){
Try {
URL url = new URL (urlstr );
Inputstream o = URL. openstream ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (O,
"GBK "));
String line = NULL;
While (line = Br. Readline ())! = NULL ){
Matcher M = newspat. matcher (line );
If (M. Matches ()){
Newslist. Add (line );
System. Out. println (line );
Getmain (line );
}
}
} Catch (ioexception e ){
E. printstacktrace ();
}
}
Public void getmain (string line) throws malformedurlexception {
String newscontent = NULL;
Int indexofhrefbegin = line. indexof ("HTTP ");
Int indexofhrefend = line. indexof ("shtml ");
Int indexoftitlebegin = line. indexof (">", indexofhrefbegin );
Int indexoftitleend = line. indexof ("</a> ");
String title = line. substring (indexoftitlebegin + 1, indexoftitleend );
System. Out. println (title );
String hrefstr = line. substring (indexofhrefbegin, indexofhrefend + 5 );
URL u = new URL (hrefstr );
Try {
Inputstream o = U. openstream ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (O,
"GBK "));
String content = NULL;
Stringbuffer Sf = new stringbuffer ();
While (content = Br. Readline ())! = NULL ){
Matcher M = mainpat. matcher (content );
If (M. Matches ()){
SF. append (content );
Newscontent = SF. tostring ();
}
}
If (SF! = NULL ){
New n = New ();
N. setid (I );
I ++;
N. setcontent (newscontent );
N. settitle (title );
News. Add (N );
}
} Catch (ioexception e ){
E. printstacktrace ();
}
}
Public void savatodb (){
Newsdao. savenews (News );
}
}
Persistence class:
Package Dao;
Import java. SQL. connection;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. util. List;
Import DTO. New;
Public class newsdao {
Connection con = dbconnection. getconnection ();
Preparedstatement PST = NULL;
Resultset rs = NULL;
Public void savenews (list <New> News ){
Try {
String SQL = "insert into news values (?,?,?) ";
Pst = con. preparestatement (SQL );
For (new N: News ){
Pst. setint (1, N. GETID ());
Pst. setstring (2, N. gettitle ());
Pst. setstring (3, N. getcontent ());
Pst.exe cuteupdate ();
}
} Catch (sqlexception e ){
E. printstacktrace ();
} Finally {
Try {
Pst. Close ();
Con. Close ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
}
}
Database Connection class:
Package Dao;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Public class dbconnection {
Private Static final string dbdriver = "com. MySQL. JDBC. Driver ";
Private Static final string dburl = "JDBC: mysql: // localhost: 3306/newsstealing? Useunicode = true & characterencoding = GBK ";
Private Static final string dbuser = "root"; // User Name
Private Static final string Password = "pass"; // Password
Private Static connection = NULL;
Static {
Try {
Class. forname (dbdriver );
} Catch (classnotfoundexception e ){
E. printstacktrace ();
}
}
Public static connection getconnection (){
Try {
Connection = drivermanager. getconnection (dburl, dbuser, password );
} Catch (exception e ){
E. printstacktrace ();
}
Return connection;
}
}
All classes are here. Build a web project based on the class name and package name. Put the MySQL driver under Lib. Create a database and a table in the database (the name can be found in the database connection class and Dao ). You can use different databases according to your own conditions. Because the function is not complete yet, all data except the ID must be allowed to be null when creating the table. Otherwise, some news content cannot be obtained during insertion, and an error will occur.