Home Java javaTutorial Write Java Zhihu crawler from scratch to obtain content recommended by Zhihu editors

Write Java Zhihu crawler from scratch to obtain content recommended by Zhihu editors

Dec 24, 2016 am 11:18 AM

First spend three to five minutes designing a Logo =. =As a programmer, I have always wanted to be an artist!
Write Java Zhihu crawler from scratch to obtain content recommended by Zhihu editors

Okay, it’s a bit small to make, so I’ll make do with it for now.

Next, we start making Zhihu’s crawler.

First of all, determine the first goal: editor recommendation.

Webpage link: http://www.zhihu.com/explore/recommendations

We have slightly modified the last code to obtain the content of the page:

import java.io.*;
import java.net.*;
import java.util.regex.*;
public class Main {
 static String SendGet(String url) {
  // 定义一个字符串用来存储网页内容
  String result = "";
  // 定义一个缓冲字符输入流
  BufferedReader in = null;
  try {
   // 将string转成url对象
   URL realUrl = new URL(url);
   // 初始化一个链接到那个url的连接
   URLConnection connection = realUrl.openConnection();
   // 开始实际的连接
   connection.connect();
   // 初始化 BufferedReader输入流来读取URL的响应
   in = new BufferedReader(new InputStreamReader(
     connection.getInputStream()));
   // 用来临时存储抓取到的每一行的数据
   String line;
   while ((line = in.readLine()) != null) {
    // 遍历抓取到的每一行并将其存储到result里面
    result += line;
   }
  } catch (Exception e) {
   System.out.println("发送GET请求出现异常!" + e);
   e.printStackTrace();
  }
  // 使用finally来关闭输入流
  finally {
   try {
    if (in != null) {
     in.close();
    }
   } catch (Exception e2) {
    e2.printStackTrace();
   }
  }
  return result;
 }
 static String RegexString(String targetStr, String patternStr) {
  // 定义一个样式模板,此中使用正则表达式,括号中是要抓的内容
  // 相当于埋好了陷阱匹配的地方就会掉下去
  Pattern pattern = Pattern.compile(patternStr);
  // 定义一个matcher用来做匹配
  Matcher matcher = pattern.matcher(targetStr);
  // 如果找到了
  if (matcher.find()) {
   // 打印出结果
   return matcher.group(1);
  }
  return "Nothing";
 }
 public static void main(String[] args) {
  // 定义即将访问的链接
  String url = "http://www.zhihu.com/explore/recommendations";
  // 访问链接并获取页面内容
  String result = SendGet(url);
  // 使用正则匹配图片的src内容
  //String imgSrc = RegexString(result, "src=\"(.+?)\"");
  // 打印结果
  System.out.println(result);
 }
}
Copy after login

There will be no problem if you run it, the next step is A regular matching problem.

First, let’s get all the questions on this page.

Right-click the title and inspect the element:

Write Java Zhihu crawler from scratch to obtain content recommended by Zhihu editors

Aha, you can see that the title is actually an a tag, which is a hyperlink, and the thing that can be distinguished from other hyperlinks should be the class. That is, the class selector.

So our regular statement comes out: question_link.+?href="(.+?)"

Call the RegexString function and pass it parameters:

public static void main(String[] args) {
  // 定义即将访问的链接
  String url = "http://www.zhihu.com/explore/recommendations";
  // 访问链接并获取页面内容
  String result = SendGet(url);
  // 使用正则匹配图片的src内容
  String imgSrc = RegexString(result, "question_link.+?>(.+?)<");
  // 打印结果
  System.out.println(imgSrc);
 }
Copy after login

Aha, you can see that we successfully caught one Title (note, it’s just one):
Write Java Zhihu crawler from scratch to obtain content recommended by Zhihu editors

Wait a minute, what is this mess? !

Don’t be nervous=. =It's just garbled characters.

For encoding issues, please refer to: HTML character set

Generally speaking, the mainstream encodings with better support for Chinese are UTF-8, GB2312 and GBK encoding.



The above is the content of writing Java Zhihu crawler with zero foundation to obtain content recommended by Zhihu editors. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles