Table of Contents
回复内容:
Home Backend Development PHP Tutorial 怎么让爬虫智能的爬取网页的文章内容

怎么让爬虫智能的爬取网页的文章内容

Jun 06, 2016 pm 08:35 PM
php python algorithm web crawler

相關問題:如何提取網頁正文

最近写了一个爬虫 都过正则来匹配一个文章的内容这样有些麻烦,每个网站都要写正则。
1,怎么智能的实现网页文章内容的爬取?需要怎么做?
eg:
http://www.cnbeta.com/articles/385387.htm
http://www.ifanr.com/512005
2,抓取完以后怎么提取文章的标签?用于后于后面的相似文章推荐。

回复内容:

相關問題:如何提取網頁正文

最近写了一个爬虫 都过正则来匹配一个文章的内容这样有些麻烦,每个网站都要写正则。
1,怎么智能的实现网页文章内容的爬取?需要怎么做?
eg:
http://www.cnbeta.com/articles/385387.htm
http://www.ifanr.com/512005
2,抓取完以后怎么提取文章的标签?用于后于后面的相似文章推荐。

第一個問題和已有問題重複:如何识别並提取网页正文?。

第二個問題我寫過簡單的分詞算法,順便把出現頻率高的詞作爲關鍵詞提取了。即便是非常簡單的算法,對大多數網頁效果也還不錯。

不過分詞算法有很多現成的,你可以搜;
關鍵詞提取有很多現成的,你可以搜。。。

第二問貌似也與已有問題重複。

我之前用php写过一个采集插件。你这个叫网页正文提取。

算法大概如下:

1、把网页分解成很多DOM块。
2、分解的dom块。你要用一定的标准去丢弃,排除。比如有些dom快里面,都是大堆的链接这个一般是列表。可以丢弃。还有 算出文本密度(文本/html)的比值。比如(span,p,a,font)等标签所占的百分比。等等,经过多重过滤,最终会留下不多的几个dom块。再按照一定的规则过滤掉。正确率会比较高。

最重要的一个值也可以作为参考,我在一篇论文里面看到,用一段文字里面的句号多少来判定。
如果一大段文字里面,句号出现很多,那这个dom快很可能是内容快。

我之前写过一个java版的爬虫(Gworm),提出一点拙见,如果随便给你一个网址,智能的提取出网页的文章部分还是很困难的(也不是没方法吧,要用到统计概率的方法,也不能做到百分之百正确)。所以我之前的方案是使用css选择器提取内容,而不自己手写正则表达式,一个网站的css样式名一般都是很稳定的,这样一来一个网站的所有文章只需要一个提取规则,而且你第二个问题,获取文章标签,使用css选择器也可以轻松解决。介于题主使用python爬取,我不清楚python有什么库可以提供对DOM进行css选择的功能,但我相信一定是有的,对应我使用java版css选择器是Jsoup。

更新:刚google一下“python css selector”一大把结果。看下这篇文章吧https://pythonhosted.org/cssselect/。

python中有 pyquery
php有 phpquery
都很方便用jquery语法处理,

python中有scrapy框架,很不错,还有一个scrapinghub的云平台,可以省掉你的很多工作;

至于抓取标签,就涉及分类和聚类算法,这里面就有很多选择了

数据量不大的话,用 readability 的 api,省事。

https://www.readability.com/developers/api/parser

建议不要用正则来做html解析,学习一下lxml,然后在chrome浏览器的开发这模式就可以复制对应DOM节点的xpath直接在lxml,省了很多事,而且lxml解析html,xml性能杠杠的

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP and the Web: Exploring its Long-Term Impact PHP and the Web: Exploring its Long-Term Impact Apr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles