Data crawling method, device, electronic device and storage medium
By determining the web page address and link tags, crawling and parsing the web content layer by layer, it solves the problem of unclear hierarchical relationships in traditional data crawling, realizes automatic crawling and deduplication of complete web content, and improves data collection efficiency and accuracy.
Patent Information
- Application Number
- CN202311785264.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-22
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2043-12-22
AI Technical Summary
Traditional data crawling programs cannot clarify the hierarchical relationship between links, resulting in chaotic crawling, inability to obtain complete web content, and difficulty in data collection.
By determining the web page addresses and link tags that need to be crawled, determining the hierarchical relationship between links, crawling web page information layer by layer, and using multi-layer structure parsing and deduplication mechanisms to achieve automatic crawling of web page information.
Obtain the complete web page content, avoid the crawler from falling into an infinite loop, bypass the anti-crawling strategy, and achieve efficient and stable information crawling.
Smart Images

Figure CN117891992B
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the field of data acquisition technology, and in particular to a data crawling method, device, electronic device and storage medium. Background Art
[0002] A web crawler is an automated web crawling tool that can crawl and collect data from the Internet according to certain rules and algorithms. The technology of web crawlers continues to develop and has become an important tool in application fields such as data collection, information processing, and search engines.
[0003] In traditional crawling programs, the crawled links cannot be graded due to the inability to clearly define the hierarchical relationship between links, resulting in crawling confusion, crawling only part of the linked web page content, and failing to crawl the complete web page content, making data collection difficult. Summary of the invention
[0004] In order to solve the technical problem that the hierarchical relationship between links cannot be clearly defined and the crawled links cannot be graded in the existing data crawling process, the embodiments of the present invention provide a data crawling method, device, electronic device and storage medium.
[0005] The technical solution of the embodiment of the present invention is achieved as follows:
[0006] An embodiment of the present invention provides a data crawling method, characterized in that the method includes: determining a web page address to be crawled and a link tag to be extracted; determining a hierarchical relationship between links based on the web page address to be crawled and the link tag to be extracted; crawling layer by layer according to the hierarchical relationship, and saving the crawled web page information.
[0007] In one embodiment, the hierarchical relationship between links is determined according to the web page address to be crawled and the link tag to be extracted, including: passing the web page address to be crawled and the link tag to be extracted into a defined first function; the first function is used to obtain web page content using a first library according to the web page address to be crawled and the link tag to be extracted; the first library is used to send a request to the web page address to be crawled to obtain web page content; the web page content is parsed using a second library, and the web page content is converted into a multi-layer structure; the nodes of the multi-layer structure include link tags; traversing the link tags, for the link tags containing the link tags of the next layer of links, continuing to call the first function to obtain the web page content of the next layer of links; and so on, until the link tags of the converted multi-layer structure do not contain the link tags of the next layer of links.
[0008] In one embodiment, the traversing of the link tags, for the link tags containing the next layer link, continuing to call the first function to obtain the web page content of the next layer link, includes: traversing the link tags, and determining whether the type of the link tag is a link type; when determining that the type of the link tag is a link type, obtaining a first attribute value in the link tag; using the first attribute value as the web page address of the next layer link, and calling the first function to obtain the web page content of the next layer link.
[0009] In one embodiment, after traversing the link tag and determining whether the type of the link tag is a link type, the method further includes: when determining that the type of the link tag is a resource type, obtaining and storing a corresponding resource file according to the link tag.
[0010] In one embodiment, obtaining and storing the corresponding resource file according to the link tag includes: determining the storage location of the resource file according to the link tag; when it is determined that the storage location is a website or a server, obtaining the corresponding resource file by sending a request to the website or the server; when it is determined that the storage location is a local file system, directly accessing the file path to obtain the corresponding resource file.
[0011] In one embodiment, after determining the hierarchical relationship between links based on the web page addresses to be crawled and the link tags to be extracted, the method further includes: selecting at least one deduplication method from a plurality of preset deduplication methods; and deduplicating the links based on the selected deduplication method.
[0012] In one embodiment, the preset multiple deduplication methods include: obtaining the web page address corresponding to each link, determining whether the web page address is repeated, and removing the links corresponding to the repeated web page addresses; or, obtaining the web page content corresponding to each link, performing hash processing on the web page content, obtaining hash values, determining whether the hash values are the same, and removing the links corresponding to the web page content with the same hash values; or, obtaining the fingerprint information of the web page corresponding to each link, and removing the links corresponding to the web pages with the same fingerprint information.
[0013] An embodiment of the present invention also provides a data crawling device, which includes: a first determination module, used to determine the web page address that needs to be crawled and the link tag that needs to be extracted; a second determination module, used to determine the hierarchical relationship between links based on the web page address that needs to be crawled and the link tag that needs to be extracted; and a crawling module, used to crawl layer by layer according to the hierarchical relationship and save the crawled web page information.
[0014] An embodiment of the present invention further provides an electronic device, comprising: a sensor, a processor, and a memory for storing a computer program that can be run on the processor; wherein the processor executes the steps of any of the above methods when running the computer program.
[0015] An embodiment of the present invention further provides a storage medium, in which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above methods are implemented.
[0016] The data crawling method, device, electronic device and storage medium provided by the embodiment of the present invention determine the web page address to be crawled and the link tag to be extracted; determine the hierarchical relationship between links according to the web page address to be crawled and the link tag to be extracted; crawl layer by layer according to the hierarchical relationship, and save the crawled web page information. The solution provided by the present invention can obtain the complete web page content by defining the link relationship and traversing each level, rather than being limited to the partial content of the current page, so as to realize the automatic crawling of web page information. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 This is a flow chart of a data crawling method according to an embodiment of the present invention;
[0018] Figure 2 An example diagram of a process for defining a link relationship according to an embodiment of the present invention;
[0019] Figure 3 This is a schematic diagram of the structure of a data crawling device according to an embodiment of the present invention;
[0020] Figure 4 1 is a diagram showing the internal structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0021] The present invention will be described in further detail below with reference to the accompanying drawings and embodiments.
[0022] The embodiment of the present invention provides a data crawling method, such as Figure 1 As shown, the method includes:
[0023] Step 101: Determine the web page address to be crawled and the link tag to be extracted;
[0024] Step 102: determining the hierarchical relationship between links according to the web page address to be crawled and the link tag to be extracted;
[0025] Step 103: crawl layer by layer according to the hierarchical relationship, and save the crawled web page information.
[0026] This embodiment can be applied to the benchmark test public service platform to achieve dynamic layer-by-layer crawling of content in the form of file streams. This embodiment can collect web page information on the Internet and perform data analysis based on the web page information.
[0027] By defining link relationships and traversing each level, this embodiment can obtain the complete content of the web page instead of only obtaining part of the content of the current page; it can extract more data instead of being limited to the data of the current page; it can prevent the crawler from falling into an infinite loop, thereby ensuring the normal operation of the crawler; it can simulate human behavior, thereby bypassing anti-crawling strategies and obtaining required data.
[0028] Specifically, in one embodiment, determining the hierarchical relationship between links according to the web page address to be crawled and the link tag to be extracted includes:
[0029] The web page address to be crawled and the link tag to be extracted are passed into the defined first function; the first function is used to obtain web page content using the first library according to the web page address to be crawled and the link tag to be extracted; the first library is used to send a request to the web page address to be crawled to obtain web page content;
[0030] Parsing the webpage content using the second library to convert the webpage content into a multi-layer structure; the nodes of the multi-layer structure include link tags;
[0031] The link tags are traversed, and for the link tags containing the next-layer links, the first function is continuously called to obtain the webpage content of the next-layer links; and so on, until the link tags of the converted multi-layer structure do not contain the link tags of the next-layer links.
[0032] Here, the first function may be a get_html function. The get_html function has two parameters, URL and Label, which may be used to input the webpage address to be crawled (also called webpage URL) and the link label to be extracted.
[0033] The first library may be a request library, and the requests library may send an HTTP request to the web page address to be crawled in the get_html function, thereby obtaining the web page content. HTML is a markup language used to describe the structure of a web page. The crawler needs to parse HTML to extract data from the web page.
[0034] The second library can be the BeautifulSoup library. The BeautifulSoup library can parse web pages, and the BeautifulSoup library will convert complex HTML documents into a multi-layer structure. Here, the multi-layer structure can be a tree structure. Each node of the tree structure is an object, and these objects can include various HTML tags. There can be a sequential relationship and a hierarchical relationship (i.e., a parent-child relationship and a brother relationship) between the nodes in a tree structure. The first level of the tree structure can hold the top-level nodes, the second level holds the child nodes, and so on. For example, the top level of the tree structure can be the outermost tag of HTML, which is the root node, and the tags contained in the top-level tag are its child tags. The child tags can be placed in the second level of the tree structure. When these tags are converted into a tree structure, there will be multiple levels.
[0035] Here is a simple example (excerpt):
[0036]
[0037] For each link, it can be determined whether the link is a link type or a resource type. If the link is a link type, the sub-tags of the next layer contained in the link can be obtained, and then the data of the next layer can be crawled to improve the link hierarchy. If the link is a resource type, the corresponding resource file can be obtained through the link to complete the data crawling process.
[0038] Specifically, in one embodiment, traversing the link tags, for a link tag containing a next-layer link in the link tags, continuing to call the first function to obtain the webpage content of the next-layer link includes:
[0039] Traversing the link tag to determine whether the type of the link tag is a link type;
[0040] When it is determined that the type of the link tag is a link type, a first attribute value in the link tag is obtained; the first attribute value is used as the webpage address of the next layer link, and a first function is called to obtain the webpage content of the next layer link.
[0041] When it is determined that the type of the link tag is a resource type, a corresponding resource file is acquired according to the link tag and stored.
[0042] Here, the type of link tag can be determined based on the suffix of the link tag. For example, if the extracted link is still an HTML page, it means that the link is a link type. According to the href attribute value (i.e., the first attribute value) in the link tag, the first function is called to continue extracting the link of the next level. If the extracted link is a resource type link (e.g., PDF, zip, etc.), the corresponding resource file (e.g., PDF, zip, etc.) is obtained and stored.
[0043] Here, when the link is of link type, the link tag is an a tag. Each a tag has an href attribute value. The href attribute value can be used as a link to continue crawling deeper. The following is a complete a tag example:
[0044] <a href="www.baidu.com"> Baidu
[0045] The content in quotation marks in the example is the attribute value.
[0046] That is, when the link is of link type, the href attribute value in the link tag is the next level link, so that the web page content of the next level link can be continued to be crawled.
[0047] In one embodiment, the step of obtaining and storing the corresponding resource file according to the link tag includes:
[0048] Determine the storage location of the resource file according to the link tag;
[0049] When it is determined that the storage location is a website or a server, acquiring the corresponding resource file by sending an acquisition request to the website or the server;
[0050] When it is determined that the storage location is a local file system, the file path is directly accessed to obtain the corresponding resource file.
[0051] That is, if the storage location is a website or FTP server, you can use HTTP or FTP protocol to send a request to obtain the corresponding resource file. If the storage location is a local file system, you can directly access the file path to obtain the corresponding resource file.
[0052] This embodiment can realize normal access to some resource contents such as pictures, videos, attachments, etc.
[0053] In one embodiment, after determining the hierarchical relationship between links according to the web page address to be crawled and the link tag to be extracted, the method further includes:
[0054] Select at least one deduplication method from among multiple preset deduplication methods;
[0055] The links are deduplicated based on the selected deduplication method.
[0056] Here, the preset multiple deduplication methods may include: obtaining the web page address corresponding to each link, determining whether the web page address is repeated, and removing the links corresponding to the repeated web page addresses; or, obtaining the web page content corresponding to each link, performing hash processing on the web page content, obtaining hash values, determining whether the hash values are the same, and removing the links corresponding to the web page content with the same hash values; or, obtaining the fingerprint information of the web page corresponding to each link, and removing the links corresponding to the web pages with the same fingerprint information.
[0057] Here, the fingerprint information may be a request header of a web page. The request header is unique and can be used to deduplicate content.
[0058] In actual application, the above deduplication method can be specifically:
[0059] 1. Save the visited URLs in the database. Each time you crawl, search the current URL among the URLs saved in the database. If there are duplicates, skip the duplicate URLs to achieve the purpose of deduplication.
[0060] 2. Use the non-repetitive feature of HashSet to remove duplicates. HashSet is a collection class whose internal implementation is based on a hash table and has the characteristic of not containing duplicate elements (the elements here can be URLs, content, fingerprint information, and the elements will not be repeated below). The collection will automatically handle duplicate elements to ensure that each element is unique. The advantage is that it is easy to understand and easy to use.
[0061] 3. The URL is hashed by md5 and other methods and saved in the set to achieve deduplication. The specific implementation is: first, the link obtained by the automatic crawling submodule is read out as a string, and then a hash function (such as MD5) is used to calculate each URL to obtain a hash value of a fixed length. Finally, the URL with the calculated hash value is added to the set, so that the elements in the set are the visited URLs. Due to the characteristics of the set, duplicate URLs will not be added repeatedly.
[0062] 4. Use the bitmap method, which uses a bitmap to represent the data status. The elements are mapped to integers through a hash function, and each bit of the binary representation of the integer is used as a marker in the bitmap. The marker bit corresponding to the visited element is set to 1, and the marker bit corresponding to the unvisited element is set to 0. This allows you to quickly check whether the element has been visited, saving space and improving the efficiency of duplicate checking.
[0063] 5. Use Redis's set to deduplicate. Store the URL's hash value in Redis. Before processing each new link, use the hash function to calculate the URL's hash value, and then check whether the hash value is in Redis's Set. If it is, it means that the URL has been visited and will not be processed again; if it is not, it means that the URL is new and can be added to the Set. The advantage is that it is fast (Redis itself is fast), and it will not occupy the resources of the crawler server, and can handle data crawling of larger data volumes.
[0064] 6. Deduplication can also be achieved using Bloom filters. Bloom filters detect whether an element may exist by using bit arrays and hash functions. In the deduplication process, a bit array is first created and initialized to 0. For each element to be inserted, multiple hash functions are used to map to different positions in the bit array, and the values of these positions are set to 1. In the query phase, if all the bits corresponding to the hash functions of the element are 1, it is determined that the element may exist, otherwise it can be determined that the element must not exist, achieving efficient deduplication. This method saves memory and can provide fast and scalable deduplication in large-scale data processing.
[0065] This embodiment can achieve a deduplication function for links or contents that may be duplicated when crawled.
[0066] This embodiment can realize an efficient and stable information crawling function by analyzing the link relationship in detail, automatically filtering and removing duplicates, and saving the information, attachments, pictures, videos and other web page information in the web page.
[0067] The data crawling method provided by the embodiment of the present invention determines the web page address to be crawled and the link tag to be extracted; determines the hierarchical relationship between links according to the web page address to be crawled and the link tag to be extracted; crawls layer by layer according to the hierarchical relationship, and saves the crawled web page information. The solution provided by the present invention can obtain the complete web page content by defining the link relationship and traversing each level, rather than being limited to the partial content of the current page, thereby realizing the automatic crawling of web page information.
[0068] The present invention will be described in detail below in conjunction with application examples.
[0069] The present invention provides a system and method for dynamically crawling content in the form of file streams for a benchmark test public service platform. Figure 2 .
[0070] The system consists of three subsystems, each of which has its own functions and can effectively realize dynamic content crawling.
[0071] 1. Automatic crawling submodule
[0072] Functional description:
[0073] 1. Get the complete web page content: By defining the link relationship and traversing each level, you can get the complete content of the web page instead of just getting part of the current page.
[0074] 2. Extract more data: By traversing each level, more data can be extracted instead of being limited to the data on the current page.
[0075] 3. Avoid infinite loop: By defining link relationships and traversing each level, you can avoid the crawler from falling into an infinite loop, thereby ensuring the normal operation of the crawler.
[0076] 4. Solve the anti-climbing strategy: By defining the link relationship and traversing each level, you can simulate human behavior, thereby bypassing the anti-climbing strategy and obtaining the required data.
[0077] Functionality:
[0078] Link relationships and perform batch extraction. The specific implementation steps are as follows:
[0079] 1. Define the get_html function: Define a get_html() function, pass in the URL of the web page to be crawled and the link tag to be extracted (such as the a tag). This function is responsible for loading the request library to obtain the web page content.
[0080] 2. Use the requests library to obtain web page content: Use the requests library to send an HTTP request to the URL specified in the get_html function to obtain the web page content.
[0081] 3. Use the BeautifulSoup library to parse web pages: Use the BeautifulSoup library to parse web pages. BeautifulSoup will convert complex HTML documents into a tree structure. Each node is an object. These objects include various HTML tags to ensure that the target link can be easily extracted later.
[0082] 4. Define an empty list: Define an empty list to store the extracted links.
[0083] 5. Traverse link tags: Traverse the link tags that need to be extracted. The href attribute value in the tag is an external link, and its href attribute value is added to the list.
[0084] 6. Recursively call the function: For each link in the list, recursively call the function to continue extracting the links of the next level.
[0085] 7. Further process the links: After the traversal is completed, the link type is determined based on the suffix name, and the extracted links can be further processed. If the extracted links still have HTML pages, continue to execute from the first step to the seventh step of the automatic crawling submodule. If the extracted link is a link to a resource file, first store the link of the resource file (for example: PDF, zip, etc.) and reserve it for the subsequent resource file management submodule.
[0086] 2. Comparison of Deduplication Submodules
[0087] Functional description:
[0088] Deduplication by comparison refers to the process of comparing and removing duplicate data during the crawling process to ensure that the acquired data set is unique. This function is very important in crawlers, because when web crawlers crawl web page data, they often encounter a lot of duplicate information, which takes up a lot of storage space and data processing time, so deduplication by comparison is required.
[0089] The system can choose three deduplication methods: URL-based deduplication, content-based deduplication, and fingerprint-based deduplication.
[0090] Users can choose between two deduplication levels: normal deduplication and advanced deduplication.
[0091] Ordinary deduplication is only based on URL, which has the advantages of fast speed, simplicity and convenience.
[0092] Advanced deduplication allows you to select multiple deduplication methods from any of the three deduplication methods, which can achieve more detailed deduplication.
[0093] 1. URL-based deduplication: For each web page, its URL is unique, so we can determine whether it is duplicate data by checking whether the URL already exists in the dataset. This method is simple and intuitive, but it may miss some web pages with the same content but different URLs.
[0094] 2. Content-based deduplication: For web pages with similar content but different URLs, deduplication can be performed by comparing the web page content. This method requires comparing the hash values of the web page content and using a hash function to process the web page content to obtain a hash value of a fixed length. If the hash values of two web pages are the same, it can be assumed that their contents are the same. In this way, deduplication can be performed on files with duplicate content. This may be time-consuming for large-scale data processing.
[0095] 3. Fingerprint-based deduplication: This method is to deduplicate by extracting the fingerprint information of the web page (such as the request header, etc.). Fingerprint information data is unique, and web pages with the same fingerprint information are considered duplicates. This method has a good effect when processing large-scale data.
[0096] Functionality:
[0097] According to the given judgment basis and the given deduplication container, which stores the crawled links and information (webpage content, fingerprint), the original data is judged one by one to determine whether the data is in the deduplication container. If not, the data is added to the deduplication container according to the judgment basis in the deduplication level and method selected by the user, and the data is marked as non-duplicate data; if it is, it is not added and the data is marked as duplicate data. The specific implementation is the following multiple ways:
[0098] 1. Save the visited URLs in the database. Search the current URL in the URLs saved in the database each time you crawl. If there are duplicates, skip the duplicate URLs to achieve the purpose of deduplication. This deduplication method is the default deduplication method in the normal deduplication level.
[0099] 2. Use the non-repetitive feature of HashSet to remove duplicates. HashSet is a collection class whose internal implementation is based on a hash table and has the characteristic of not containing duplicate elements (the elements here can be URLs, content, fingerprint information, and the elements will not be repeated below). The collection will automatically handle duplicate elements to ensure that each element is unique. The advantage is that it is easy to understand and easy to use. This method is an optional deduplication method in the advanced deduplication level.
[0100] 3. The URL is hashed by md5 and other methods and saved in the set to achieve deduplication. The specific implementation is: first read the link obtained by the automatic crawling submodule as a string, and then use the hash function (such as MD5) to calculate each URL to obtain a hash value of a fixed length. Finally, add the URL with the calculated hash value to the set, so that the elements in the set are the visited URLs. Due to the characteristics of the set, duplicate URLs will not be added repeatedly. This method is an optional deduplication method in the advanced deduplication level.
[0101] 4. Use the bitmap method, which uses a bitmap to represent the data state. The elements are mapped to integers through a hash function, and each bit of the binary representation of the integer is used as a marker in the bitmap. The marker bit corresponding to the visited element is set to 1, and the marker bit corresponding to the unvisited element is set to 0. This allows you to quickly check whether the element has been visited, saving space and improving the efficiency of duplicate checking. This method is an optional deduplication method in the advanced deduplication level.
[0102] 5. Use Redis's set to deduplicate. Store the URL's hash value in Redis. Before processing each new link, use the hash function to calculate the URL's hash value, and then check whether the hash value is in Redis's Set. If it is, it means that the URL has been visited and will not be processed again; if it is not, it means that the URL is new and can be added to the Set. The advantage is that it is fast (Redis itself is fast), and it does not occupy the resources of the crawler server, and can handle data crawling of larger data volumes. This method is an optional deduplication method in the advanced deduplication level.
[0103] 6. Deduplication can also be achieved using Bloom filters. Bloom filters detect whether an element may exist by using bit arrays and hash functions. In the deduplication process, a bit array is first created and initialized to 0. For each element to be inserted, multiple hash functions are used to map to different positions of the bit array, and the values of these positions are set to 1. In the query stage, if the bits corresponding to all hash functions of the element are 1, it is determined that the element may exist, otherwise it can be determined that the element must not exist, achieving efficient deduplication. This method saves memory and can provide fast and scalable deduplication functions in large-scale data processing. This method is used as an optional deduplication method in the advanced deduplication level.
[0104] 3. Resource File Management Submodule
[0105] Functional description:
[0106] It can crawl different resource types, including files, videos, pictures, and audio.
[0107] This function can analyze the HTML structure of the webpage obtained in the automatic crawling submodule, and can be judged by different HTML tags or file suffixes to find the URL links of files, videos, and pictures, and automatically download them. For example, some websites provide file download functions. The download link is generally located in the detailed information page of the file. The file type is determined by judging the suffix name of the link, such as: pdf, zip, etc. The crawler can automatically download files by parsing these pages and extracting the download link.
[0108] Functionality:
[0109] 1. Determine the file storage location: First, you need to use the label classified as a resource link in the automatic crawling submodule to determine where the file to be crawled is stored, such as a website, FTP server, local file system, etc.
[0110] 2. Determine the file format: Determine the file format to be crawled through the suffix of the href attribute value in the link tag, such as HTML, PDF, Word, Excel, etc.
[0111] 3. Send a request to get the file: Send a request to the target location to get the file. If you use a website or FTP server, you need to use HTTP or FTP protocol to send the request. If you use the local file system, you can directly access the file path.
[0112] 4. Store files: Store files in the local file system or database for subsequent processing and analysis.
[0113] 5. Repeat: If you need to crawl multiple files, you can write a loop to repeat the above steps until all target files are crawled.
[0114] Advantages of the system and method of this embodiment include:
[0115] (1) Detailed link analysis: A link analysis algorithm based on graph theory is used to replace the existing detailed link analysis method. By considering web page links as edges in a graph and web pages as nodes in a graph, a graph theory algorithm is used to perform a detailed analysis of web page links and automatically store them in a hierarchical manner. That is, the link relationship is analyzed in detail through the entry, and the hierarchical storage is automatically performed. Snapshots are generated to store web page information, making it easier to manage links and store web pages.
[0116] (2) Deduplication filtering: Use data structures such as hash tables or Bloom filters to implement deduplication filtering. Hash tables can quickly store and search for data, while Bloom filters can achieve fast deduplication filtering in a smaller space. That is, it has a deduplication filtering function and can compare and filter the crawled content. The same page can be compared and updated based on historical records, which improves the quality and accuracy of the data.
[0117] (3) Crawling multiple types of content: Use existing crawler libraries, such as Scrapy, BeautifulSoup, and lxml, to expand the functions of these libraries to crawl multiple types of content. For example, you can write a custom parser to process file-type content such as pictures, videos, and attachments, and store them in corresponding folders. That is, you can crawl and organize various types of file-type content such as pictures, videos, and attachments, which is more comprehensive and diverse.
[0118] In order to implement the method of the embodiment of the present invention, the embodiment of the present invention also provides a data crawling device, such as Figure 3 As shown, the data crawling device 300 includes: a first determination module 301, a second determination module 302 and a crawling module 303; wherein,
[0119] The first determination module 301 is used to determine the web page address to be crawled and the link tag to be extracted;
[0120] A second determination module 302 is used to determine the hierarchical relationship between links according to the web page address to be crawled and the link tag to be extracted;
[0121] The crawling module 303 is used to crawl layer by layer according to the hierarchical relationship and save the crawled web page information.
[0122] In actual application, the first determination module 301, the second determination module 302 and the crawling module 303 can be implemented by a processor in the data crawling device.
[0123] It should be noted that: when the above-mentioned device provided in the above-mentioned embodiment is executed, only the division of the above-mentioned program modules is used as an example. In actual application, the above-mentioned processing can be assigned to different program modules as needed, that is, the internal structure of the terminal is divided into different program modules to complete all or part of the above-mentioned processing. In addition, the above-mentioned device provided in the above-mentioned embodiment belongs to the same concept as the above-mentioned method embodiment. The specific implementation process is detailed in the method embodiment and will not be repeated here.
[0124] In order to implement the method of the embodiment of the present invention, the embodiment of the present invention also provides a computer program product, which includes computer instructions, and the computer instructions are stored in a computer-readable storage medium. The processor of the computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device performs the steps of the above method.
[0125] Based on the hardware implementation of the above program modules, and in order to implement the method of the embodiment of the present invention, the embodiment of the present invention also provides an electronic device (computer device). Specifically, in one embodiment, the computer device can be a terminal, and its internal structure diagram can be as follows: Figure 4As shown. The computer device includes a processor A01, a network interface A02, a display screen A04, an input device A05 and a memory (not shown in the figure) connected through a system bus. Among them, the processor A01 of the computer device is used to provide computing and control capabilities. The memory of the computer device includes an internal memory A03 and a non-volatile storage medium A06. The non-volatile storage medium A06 stores an operating system B01 and a computer program B02. The internal memory A03 provides an environment for the operation of the operating system B01 and the computer program B02 in the non-volatile storage medium A06. The network interface A02 of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor A01, the method of any one of the above embodiments is implemented. The display screen A04 of the computer device can be a liquid crystal display or an electronic ink display, and the input device A05 of the computer device can be a touch layer covered on the display screen, or a button, trackball or touchpad set on the computer device housing, or an external keyboard, touchpad or mouse, etc.
[0126] Those skilled in the art will understand that Figure 4 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0127] The device provided by an embodiment of the present invention includes a processor, a memory, and a program stored in the memory and executable on the processor. When the processor executes the program, the method of any one of the above embodiments is implemented.
[0128] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0129] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0130] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0131] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0132] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0133] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0134] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.
[0135] It can be understood that the memory of the embodiment of the present invention can be a volatile memory or a non-volatile memory, and can also include both volatile and non-volatile memories. Among them, the non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), a magnetic random access memory (FRAM), a flash memory, a magnetic surface memory, an optical disc, or a compact disc read-only memory (CD-ROM); the magnetic surface memory can be a disk memory or a tape memory. The volatile memory can be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static random access memory (SRAM), synchronous static random access memory (SSRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDRSDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM, SyncLink Dynamic Random Access Memory), and direct RAM bus random access memory (DRRAM, Direct Rambus Random Access Memory).The memories described in the embodiments of the present invention are intended to include, but are not limited to, these and any other suitable types of memories.
[0136] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0137] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.
Claims
1. A data crawling method, characterized in that: The method comprises: Determine the web page addresses that need to be crawled and the link tags that need to be extracted; Determining the hierarchical relationship between links according to the web page addresses to be crawled and the link tags to be extracted; Crawl layer by layer according to the hierarchical relationship, and save the crawled web page information; The step of determining the hierarchical relationship between links according to the web page address to be crawled and the link tag to be extracted includes: The web page address to be crawled and the link tag to be extracted are passed into the defined first function; the first function is used to obtain web page content using the first library according to the web page address to be crawled and the link tag to be extracted; the first library is used to send a request to the web page address to be crawled to obtain web page content; Parsing the webpage content using the second library to convert the webpage content into a multi-layer structure; the nodes of the multi-layer structure include link tags; Traversing the link tags, for the link tags containing the next-layer link, continue to call the first function to obtain the webpage content of the next-layer link; and so on, until the link tags of the converted multi-layer structure do not contain the link tags of the next-layer link; The traversing of the link tags, for a link tag containing a next-layer link, continuing to call the first function to obtain the webpage content of the next-layer link, includes: Traversing the link tag to determine whether the type of the link tag is a link type; In the case where it is determined that the type of the link tag is a link type, obtaining a first attribute value in the link tag; Using the first attribute value as the webpage address of the next-layer link, calling the first function to obtain the webpage content of the next-layer link; When it is determined that the type of the link tag is a resource type, a corresponding resource file is acquired according to the link tag and stored.
2. The method according to claim 1, characterized in that: The step of obtaining and storing the corresponding resource file according to the link tag includes: Determine the storage location of the resource file according to the link tag; When it is determined that the storage location is a website or a server, acquiring the corresponding resource file by sending an acquisition request to the website or the server; When it is determined that the storage location is a local file system, the file path is directly accessed to obtain the corresponding resource file.
3. The method according to claim 1, characterized in that After determining the hierarchical relationship between links according to the web page addresses to be crawled and the link tags to be extracted, the method further includes: Select at least one deduplication method from among multiple preset deduplication methods; The links are deduplicated based on the selected deduplication method.
4. The method according to claim 3, characterized in that The preset multiple deduplication methods include: Obtaining the webpage address corresponding to each link, determining whether the webpage address is repeated, and removing the links corresponding to the repeated webpage addresses; or, Obtain the webpage content corresponding to each link, perform hash processing on the webpage content, obtain hash values, determine whether the hash values are the same, and remove links corresponding to webpage contents with the same hash values; or, The fingerprint information of the web page corresponding to each link is obtained, and the links corresponding to the web pages with the same fingerprint information are removed.
5. A data crawling device, characterized in that: The data crawling device comprises: The first determination module is used to determine the web page address to be crawled and the link tag to be extracted; A second determination module is used to determine the hierarchical relationship between links according to the web page address to be crawled and the link tag to be extracted; A crawling module, used for crawling layer by layer according to the hierarchical relationship and saving the crawled web page information; The second determination module is used to determine the hierarchical relationship between links according to the web page address to be crawled and the link tag to be extracted, including: The web page address to be crawled and the link tag to be extracted are passed into the defined first function; the first function is used to obtain web page content using the first library according to the web page address to be crawled and the link tag to be extracted; the first library is used to send a request to the web page address to be crawled to obtain web page content; Parsing the webpage content using the second library to convert the webpage content into a multi-layer structure; the nodes of the multi-layer structure include link tags; Traversing the link tags, for the link tags containing the next-layer link, continue to call the first function to obtain the webpage content of the next-layer link; and so on, until the link tags of the converted multi-layer structure do not contain the link tags of the next-layer link; The traversing the link tags, for the link tags containing the next layer link, continuing to call the first function to obtain the webpage content of the next layer link, includes: Traversing the link tag to determine whether the type of the link tag is a link type; In the case where it is determined that the type of the link tag is a link type, obtaining a first attribute value in the link tag; Using the first attribute value as the webpage address of the next-layer link, calling the first function to obtain the webpage content of the next-layer link; When it is determined that the type of the link tag is a resource type, a corresponding resource file is acquired according to the link tag and stored.
6. An electronic device, characterized in that: include: A sensor, a processor and a memory for storing a computer program capable of running on the processor; wherein, When the processor is used to run the computer program, the steps of the method according to any one of claims 1 to 4 are performed.
7. A storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 4 are implemented.
