English (unofficial) translations of posts at kexue.fm
Source

Exploration of General Crawlers (II): Implementation on Forum Crawling

Translated by DeepSeek V4 Pro. Translations can be inaccurate, please refer to the original post for important stuff.

The previously discussed scheme is essentially sufficient if the crawled pages contain only a single effective area, such as blog pages or news pages. However, for websites with distinct hierarchical divisions like forums, we need further subdivision. This is because, although the aforementioned steps allow us to extract the effective text, the result is that all the text is lumped together.

Exception Tags

The purpose of numbering nodes is for subsequent clustering, so we naturally hope that the difference between numbers within the same category is as small as possible. Among many HTML tags, several only serve functions like emphasis or hyperlinking. For example, the strong tag represents bold text, the em tag represents italics, and so on. These tags only change the style of the content without changing its hierarchy. Therefore, when traversing these tags, the numbering can remain unchanged. This ensures that tags at the same level are closer to each other, maintaining the effectiveness of the clustering.

The tags we list as exceptions are shown in the table below:

Tag Meaning
p/br Line break
strong/b Bold
em Italic
font Font style
u Underline
a Hyperlink
img Insert image
h1/h2/h3/h4/h5 Heading markers

For the complete list of exception tags, please refer directly to the source code.

Differential Peak Clustering

After extracting the effective text through the previous steps and numbering it using the depth-first method described above, we obtain a sequence regarding text positions. We find that the difference between text numbers within the same module is relatively small, while the difference between text numbers of different modules is relatively large. For instance, the position numbers for the title, date, and content of the same article are very close, and the position numbers for the title, date, and reply content of the same post floor in a forum are also quite close, as shown below. This induces us to use these position numbers to further cluster the effective text.

BBS floor division

To this end, we examine the differential graph of the position sequence and describe the actual floor boundaries with red dashed lines, resulting in the following figure:

The resulting graph obtained by differencing the position index sequence, using the index of the differential sequence as the x-axis and the differential result as the y-axis.

The figure above reveals a very obvious pattern: The boundary of each floor is basically located at the local maximum of the differential sequence. Therefore, we can use this scheme to perform segmentation-based clustering, i.e., using local peaks as split points to segment the text. This clustering method has excellent properties, such as:

1. It can automatically determine the number of clusters, meaning we do not need to know in advance how many floors there are;

2. It adapts to special cases. For example, the second and third to last floors are wider than others because they contain comments within the floor (so-called “floor-within-a-floor”), leading to more content. Even so, this pattern still holds.

This indicates that using this pattern to block text and divide different floors is quite reliable. Furthermore, this clustering can be run repeatedly to adapt to different granularity requirements. In fact, for forums, running it approximately 1 \sim 2 times is usually enough to obtain the floor divisions.

Content Recognition

Finally, specifically for forums, we categorize the content of each text block. To avoid complicating the crawler and to ensure its efficiency, the recognition methods used here are relatively simple and are purely rule-based classifications.

Title

Title recognition is relatively simple because normal page source codes contain a <title> field, which basically contains complete title information. Therefore, we only need to extract the title directly using regular expressions.

Date

In Chinese forums, common date formats include:

2017-1-9 15:42

2017-1-9 15:42 (Year-Month-Day in Chinese)

3 hours ago

Yesterday 20:48

The first two date formats are easy to recognize, while the latter two are more difficult. The latter two formats mainly appear in posts published within a short period on certain forums; generally, after a day or two, they automatically revert to the first two formats.

In this case, still proceeding from the perspective of simplicity and stability, we can adopt a compromise strategy: we do not recognize the dates for posts on these websites within the short term and save them mixed with other content. From a continuous monitoring perspective, after a day or two, we will be able to recognize the date. This neither affects the real-time nature of monitoring nor makes the program overly complex.

Author

In fact, identifying the author (poster) is quite difficult, and we have not found an effective algorithm to complete a universal author recognition model. Additionally, considering that the value of identifying the poster is not particularly high, we ultimately abandoned this part of the recognition work.

Main Body

After identifying the time, the single text area is split into upper and lower halves by the time marker. We assume that one of these two halves is the main content. To identify which half contains the main body, we perform global statistics on both halves and compare which half contains more Chinese characters. The part with more characters is considered the main body.

Of course, from a more rigorous perspective, one could follow the academic approach mentioned in the previous article, using language models to determine which part is closer to natural language to identify the main body. However, this scheme is less efficient, and since we are primarily concerned with crawling Chinese content, we excluded this scheme from our experiments.

When reprinting, please include the original address of this article: https://kexue.fm/archives/4422

For more detailed reprinting matters, please refer to: “Scientific Space FAQ”