相關問題:如何提取網頁正文
最近写了一个爬虫 都过正则来匹配一个文章的内容这样有些麻烦,每个网站都要写正则。
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性能杠杠的

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.