Java Crawl Baidu Homepage logo

Source: Internet
Author: User

  • Two methods
    • A URL to get the page source code geturlcontentstring, the other one from the source code to get the desired address fragments, which need to use regular expressions to match
  • The process of getting the Web source code:
    • address is string, convert address to URL object in Java
    • The OpenConnection method of the URL returns urlconnection
    • URLConnection Connect method to establish a connection
    • Create a new InputStreamReader object , where InputStreamReader's build requires InputStream input stream objects, and URLConnection's getInputStream method returns an input stream object, so you can connect
    • . Span style= "color:black; Font-family: Microsoft Black "> Then use the established InputStreamReader object to establish the Bufferereader object
    • Read the Web page source from the BufferedReader object in line, append to the result string, the result string is the page source code string
  • Logo address Matching
    • ? Pattern pattern = pattern.compile (patternstring);
      • Java.util.regex:Java Class library package that matches a string with a pattern defined by a regular expression

        It consists of two classes: pattern and matcher.

        Pattern: Creates a matching pattern string.

        Matcher: Matches the pattern string with the input string.

      • The Compile method of pattern : Compiles the specified characters into the pattern
    • Matcher Matcher = Pattern.matcher (contentstring);

??

Package com.test;

??

Import java.io.*;

Import java.net.*;

Import java.util.regex.*;

??

Public class Baidulogo {

???? static string geturlcontentstring (String urlstring) throws Exception {

???????? String result = "";

???????? URL url = new URL (urlstring);

???????? URLConnection urlconnection = url.openconnection ();

???????? Urlconnection.connect ();

???????? InputStreamReader inputstreamreader = new inputstreamreader (

???????????????? Urlconnection.getinputstream (), "Utf-8");

???????? BufferedReader in = new BufferedReader (inputstreamreader);

???????? String Line;

???????? While (line = in . ReadLine ()) = null) {

???????????? result + = line;

???????? }

???????? return result;

???? }

??

???? static string Getlogourl (String contentstring, String patternstring) {

???????? String logourl = null;

???????? Pattern pattern = pattern.compile (patternstring);

???????? Matcher Matcher = Pattern.matcher (contentstring);

???????? if (Matcher.find ()) {

???????????? Logourl = Matcher.group (1);

???????? }

???????? return logourl;

??

???? }

??

???? Public staticvoid main (string[] args) throws Exception {

???????? // define the link that will be visited

???????? String urlstring = "http://www.baidu.com";

???????? String result = geturlcontentstring (urlstring);

???????? String patternstring = "Src=\" (. +?) \"";

???????? String contentstring = result;

???????? String Logourl = Getlogourl (contentstring, patternstring);

???????? System.out.println (logourl);

???? }

}

?

Java Crawl Baidu homepage logo

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.