Crawling data in Web pages

Source: Internet
Author: User

Here's an example of fetching data from a Web page.

In this example, only the data is obtained from the Web page, but no processing is done, only the data is saved to a TXT file.

This example is written in the Android project.

Package com.example.creepertest;


Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;


public class Controller
{
public static final String Sd_lottery_url =
"http://www.sdticai.com/zjhfx/cpmain.asp?cptype=115";

public static final String Sd_lottery_file_path = "/data/data/com.example.creepertest/test.txt";

Private BufferedWriter mbufferedwriter = null;
Private BufferedReader mbufferedreader = null;

Public Controller () {
Runnable Runnable = new Runnable () {
@Override
public void Run () {
Capturehtml ();
}
};
Thread thread = new Thread (runnable);
Thread.Start ();

}

private void capturehtml () {
try{
URL sdlotterurl = new URL (sd_lottery_url);
HttpURLConnection Httpconn =
(httpurlconnection) sdlotterurl.openconnection ();


InputStreamReader InputStreamReader =
New InputStreamReader (Httpconn.getinputstream (), "utf-8");
Mbufferedreader = new BufferedReader (InputStreamReader);

OutputStream OutputStream =
New FileOutputStream (sd_lottery_file_path,true);
OutputStreamWriter outputstreamwriter = new OutputStreamWriter (outputstream);
Mbufferedwriter = new BufferedWriter (outputstreamwriter);

String linestr = null;
while (true) {
Linestr = Mbufferedreader.readline ();
if (linestr! = null) {
Mbufferedwriter.write (LINESTR);
Mbufferedwriter.newline ();
Mbufferedwriter.flush ();
}
Else
Break
}

}
catch (Malformedurlexception e) {
E.printstacktrace ();
}
catch (IOException e) {
E.printstacktrace ();
}

finally{
try {
if (mbufferedwriter! = null)
Mbufferedwriter.close ();

if (Mbufferedreader! = null)
Mbufferedreader.close ();
} catch (Exception Exception) {
Exception.printstacktrace ();
}
}
}
}

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.