Write the source code analysis of the csdn blog extraction device by yourself: process the web page and save it as a TXT file

Source: Internet
Author: User

I posted a post yesterday morning.[Original] Write the csdn blog extraction device by yourself. The extracted files can be saved in PDF, Doc, and TXT formats.A lot of netizens are interested. Some netizens say they want to publish the source code. Here, I will focus on the core of this software.CodePost it and share it with you.

The following shows how to use a regular expression to process the captured webpage and save it as a TXT file. This code is very simple. I believe everyone can understand it.

/*****/Package com.wyp.html 2txt; import Java. io. bufferedwriter; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception;/*** @ author w397090770 * create data: 2012-7-18 * Email: wyphao.2007@163.com ** All rights reserved, review not to investigate, but in the modification of this Program You must add these annotations. Thank you * for learning and communication only. */public class saveastxt {public void html2txt (File file, string STR, String title) {// remove <SCRIPT> (.*?) </SCRIPT> // STR = Str. replaceall ("<[ss] [CC] [RR] [II] [PP] [TT]. *?>. *? <[/]? [Ss] [CC] [RR] [II] [PP] [TT]> "," #### "); // STR = Str. replaceall ("<\ B (\ W +) [\ s \ W> \"/\ '=:;.] + </\ 1> "," #### "); STR = Str. replaceall ("<\ B (\ W +) [\ s \ W> \"/'= &: _;.] + </\ 1> "," #### "); // remove <br/> STR = Str. replaceall ("<[\ s] * [BB] [RR] [\ s] * [/]? [\ S] *> ", system. getproperty ("line. separator "); // <p> is also a line feed STR = Str. replaceall ("<[\ s] * [/] * [PP]. *?> ", System. getproperty (" line. separator "); // Replace the HTML Tag STR = Str. replaceall (" <[\ s] * [/]? [A-Za-Z] *. *?> "," "); // Replace the space. Two values indicate a large space, and one value indicates a small space. Str = Str. replaceall ("", ""); STR = Str. replaceall ("", ""); // replace> <STR = Str. replaceall (">", ">"); STR = Str. replaceall ("<", "<"); // replace & STR = Str. replaceall ("&", "&"); // blank half-square STR = Str. replaceall ("blank", ""); // large white space STR = Str. replaceall ("success", ""); // double quotation mark STR = Str. replaceall ("", "\" "); // copyright STR = Str. replaceall ("", ""); // registered trademark STR = Str. replaceall ("", ""); // STR = Str. replaceall ("×", "×"); // except STR = Str. replaceall ("success", "success"); // plus sign STR = Str. replaceall ("+", "+"); // I don't know why the csdn lattice is replaced. No matter what, I also replace STR = Str. replaceall ("Grid", "Grid"); // save the file savefilename = new file (file. getabsolutepath () + file. separator + title + ". TXT "); If (! Savefilename. exists () {// save try {savefilename. createnewfile ();} catch (ioexception E1) {// todo auto-generated catch blocke1.printstacktrace (); return;} bufferedwriter BW = NULL; try {// start to save BW = new bufferedwriter (New filewriter (savefilename. getabsolutepath (), false); BW. write (STR); BW. newline ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {try {BW. flush (); BW. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}}

the above code matches the captured webpage with a regular expression, removes all webpage tags, and leaves only plain text data, then you can save the image as a TXT file. Of course, the image cannot be saved in the TXT file. Therefore, when processing the image, you can simply remove it with a regular expression.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.