Method and system for automatically extracting web page text
By filtering webpage DOM nodes and calculating the scoring model, the webpage text is automatically extracted, which solves the problem of low extraction efficiency in the existing technology, realizes efficient and intelligent webpage text extraction, and adapts to the document structure of different websites.
Patent Information
- Application Number
- CN202110081170.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-21
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2041-01-21
AI Technical Summary
The efficiency of extracting web page text in the existing technology is low, and it is difficult to adapt to the document structure of different websites. It requires manual preset expressions and cannot effectively handle a large number of web documents with complex structures.
By screening the DOM nodes of the target web page, using a preset scoring model to calculate the initial and additional scores of the DOM nodes, and selecting the node with the highest final score as the text content, a method of automatically extracting the web page text is implemented. The method includes scoring the tags, parent nodes and grandparent nodes of DOM nodes, calculating the final score according to the scoring model, adjusting the score according to the proportion of non-link text, and filtering out content nodes.
It eliminates the need for manual marking, improves the efficiency of web page text extraction, can correctly handle complex and dynamically rendered pages, and improves extraction efficiency and accuracy.
Smart Images

Figure CN112765941B_ABST
Abstract
Claims
1. A method for automatically extracting web page text, characterized in that: include: Filter the DOM nodes of the target web page and obtain candidate DOM nodes; If the length of the text content of the parent node of the candidate DOM node is greater than a preset length threshold, and the parent node of the parent node exists, then according to the label of the candidate DOM node, the label of the parent node, and the label of the parent node of the parent node, obtain the initial score of the candidate DOM node, the initial score of the parent node, and the initial score of the parent node of the parent node; According to a preset scoring model, obtaining an additional score for the candidate DOM node; According to the additional score of the candidate DOM node and the matching result of the label of the candidate DOM node and the pre-selected label, the additional score of the parent node and the additional score of the parent node of the parent node are obtained; Acquire a content node according to the final scores of all preferred DOM nodes in a preferred DOM node list, wherein the preferred DOM node list includes the candidate DOM node, the final score of the candidate DOM node, the parent node, the final score of the parent node, the parent node of the parent node, and the final score of the parent node of the parent node, wherein the final score is obtained according to the initial score and the additional score; The preset scoring model is obtained by applying the following formula: score=1+O.text.split( / [,,] / ).length*2+L+Math.min(O.text.length / 100,3)+O.textNodeCount; Wherein, score represents the additional score of the candidate DOM node; O.text.split( / [,,] / ).length represents separating the candidate DOM nodes by commas to determine the number of commas; L represents separating the candidate DOM nodes by periods to determine the number of periods; Math.min(O.text.length / 100,3) represents dividing the text length of the candidate DOM node by 100 and taking it into account in the scoring weight, which shall not exceed 3 points; O.textNodeCount represents the number of child text nodes of the candidate DOM node; The obtaining, according to the additional score of the candidate DOM node and the matching result of the label of the candidate DOM node and the pre-device selected label, the additional score of the parent node and the additional score of the parent node of the parent node comprises: If the label of the candidate DOM node matches the pre-selected label, the additional score of the candidate DOM node is used as the additional score of the parent node, and half of the additional score of the candidate DOM node is used as the additional score of the parent node of the parent node.
2. The method for automatically extracting web page text according to claim 1, characterized in that: The step of obtaining the additional score of the parent node and the additional score of the parent node of the parent node according to the additional score of the candidate DOM node and the matching result of the label of the candidate DOM node and the pre-selected label further includes: If the label of the candidate DOM node does not match the pre-selected label, half of the additional score of the candidate DOM node is used as the additional score of the parent node, and 1 / 4 of the additional score of the candidate DOM node is used as the additional score of the parent node of the parent node.
3. The method for automatically extracting web page text according to claim 1, characterized in that: The step of obtaining the content node according to the final scores of all the preferred DOM nodes in the preferred DOM node list includes: Obtaining the final score of the preferred DOM node according to the final score of the preferred DOM node and the proportion of non-link text; If the maximum value of the final scores of all the preferred DOM nodes in the preferred DOM node list is greater than a preset score threshold, the preferred DOM node with the largest final score is used as the content node.
4. The method for automatically extracting webpage text according to any one of claims 1 to 3, characterized in that: The step of screening the DOM nodes of the target webpage to obtain candidate DOM nodes includes: If the tag of the DOM node of the target webpage does not match the first preset tag, taking the DOM node of the target webpage as the initial DOM node; If the label of the initial DOM node matches the second preset class and the preset id, and does not match the third preset class and the preset id, the initial DOM node is used as a candidate DOM node; If the visible area of the candidate DOM node is greater than a preset area threshold, the candidate DOM node is used as the candidate DOM node.
5. The method for automatically extracting webpage text according to any one of claims 1 to 3, characterized in that: The DOM nodes of the target webpage are screened to obtain candidate DOM nodes, and the process also includes: The target webpage is parsed to obtain a DOM node of the target webpage.
6. A system for automatically extracting web page text, characterized in that: include: A screening module is used to screen the DOM nodes of the target web page and obtain candidate DOM nodes; an initial scoring module, configured to obtain an initial score of the candidate DOM node, an initial score of the parent node, and an initial score of the parent node of the parent node according to the tag of the candidate DOM node, the tag of the parent node, and the tag of the parent node of the parent node, if the length of the text content of the parent node of the candidate DOM node is greater than a preset length threshold and the parent node of the parent node exists; A first additional scoring module, used to obtain an additional score of the candidate DOM node according to a preset scoring model; A second additional scoring module, configured to obtain the additional score of the parent node and the additional score of the parent node of the parent node according to the additional score of the candidate DOM node and the matching result of the label of the candidate DOM node and the pre-selected label; an extraction module, configured to obtain a content node according to the final scores of all preferred DOM nodes in a preferred DOM node list, wherein the preferred DOM node list includes the candidate DOM node, the final score of the candidate DOM node, the parent node, the final score of the parent node, the parent node of the parent node, and the final score of the parent node of the parent node, wherein the final score is obtained according to the initial score and the additional score; The preset scoring model is obtained by applying the following formula: score=1+O.text.split( / [,,] / ).length*2+L+Math.min(O.text.length / 100,3)+O.textNodeCount; Wherein, score represents the additional score of the candidate DOM node; O.text.split( / [,,] / ).length represents separating the candidate DOM nodes by commas to determine the number of commas; L represents separating the candidate DOM nodes by periods to determine the number of periods; Math.min(O.text.length / 100,3) represents dividing the text length of the candidate DOM node by 100 and taking it into account in the scoring weight, which shall not exceed 3 points; O.textNodeCount represents the number of child text nodes of the candidate DOM node; The obtaining, according to the additional score of the candidate DOM node and the matching result of the label of the candidate DOM node and the pre-device selected label, the additional score of the parent node and the additional score of the parent node of the parent node comprises: If the label of the candidate DOM node matches the pre-selected label, the additional score of the candidate DOM node is used as the additional score of the parent node, and half of the additional score of the candidate DOM node is used as the additional score of the parent node of the parent node.
7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the steps of the method for automatically extracting the text of a web page as described in any one of claims 1 to 5 are implemented.
8. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method for automatically extracting the text of a web page as claimed in any one of claims 1 to 5 are implemented.
