深度剖析使用python抓取网页正文的源码
本方法是基于文本密度的方法,最初的想法来源于哈工大的《基于行块分布函数的通用网页正文抽取算法》,本文基于此进行一些小修改。
约定:
本文基于网页的不同行来进行统计,因此,假设网页内容是没有经过压缩的,就是网页有正常的换行的。
有些新闻网页,可能新闻的文本内容比较短,但其中嵌入一个视频文件,因此,我会给予视频较高的权重;这同样适用于图片,这里有一个不足,应该是要根据图片显示的大小来决定权重的,但本文的方法未能实现这一点。
由于广告,导航这些非正文内容通常以超链接的方式出现,因此文本将给予超链接的文本权重为零。
这里假设正文的内容是连续的,中间不包含非正文的内容,因此实际上,提取正文内容,就是找出正文内容的开始和结束的位置。
步骤:
首先清除网页中CSS,Javascript,注释,Meta,Ins这些标签里面的内容,清除空白行。
计算每一个行的经过处理的数值(1)
计算上面得出的每行文本数的最大正子串的开始结束位置
其中第二步需要说明一下:
对于每一行,我们需要计算一个数值,这个数值的计算如下:
一个图片标签img,相当于出现长度为50字符的文本 (给予的权重),x1,
一个视频标签embed,相当于出现长度为1000字符的文本, x2
一行内所有链接的标签 a 的文本长度 x3 ,
其他标签的文本长度 x4
每行的数值 = 50 * x1其出现次数 + 1000 * x2其出现次数 + x4 – 8
//说明, -8 因为我们要计算一个最大正子串,因此要减去一个正数,至于这个数应该多大,我想还是按经验来吧。
完整代码
代码如下:
#coding:utf-8
import re
def remove_js_css (content):
""" remove the the javascript and the stylesheet and the comment content (<script>....</script> and ) """
r = re.compile(r'''
s = r.sub ('',content)
r = re.compile(r'''
s = r.sub ('', s)
r = re.compile(r'''''', re.I|re.M|re.S)
s = r.sub('',s)
r = re.compile(r'''
s = r.sub('',s)
r = re.compile(r'''
s = r.sub('',s)
return s
def remove_empty_line (content):
"""remove multi space """
r = re.compile(r'''^\s+$''', re.M|re.S)
s = r.sub ('', content)
r = re.compile(r'''\n+''',re.M|re.S)
s = r.sub('\n',s)
return s
def remove_any_tag (s):
s = re.sub(r''']+>''','',s)
return s.strip()
def remove_any_tag_but_a (s):
text = re.findall (r''']*>(.*?)''',s,re.I|re.S|re.S)
text_b = remove_any_tag (s)
return len(''.join(text)),len(text_b)
def remove_image (s,n=50):
image = 'a' * n
r = re.compile (r'''''',re.I|re.M|re.S)
s = r.sub(image,s)
return s
def remove_video (s,n=1000):
video = 'a' * n
r = re.compile (r'''
s = r.sub(video,s)
return s
def sum_max (values):
cur_max = values[0]
glo_max = -999999
left,right = 0,0
for index,value in enumerate (values):
cur_max += value
if (cur_max > glo_max) :
glo_max = cur_max
right = index
elif (cur_max cur_max = 0
for i in range(right, -1, -1):
glo_max -= values[i]
if abs(glo_max left = i
break
return left,right+1
def method_1 (content, k=1):
if not content:
return None,None,None,None
tmp = content.split('\n')
group_value = []
for i in range(0,len(tmp),k):
group = '\n'.join(tmp[i:i+k])
group = remove_image (group)
group = remove_video (group)
text_a,text_b= remove_any_tag_but_a (group)
temp = (text_b - text_a) - 8
group_value.append (temp)
left,right = sum_max (group_value)
return left,right, len('\n'.join(tmp[:left])), len ('\n'.join(tmp[:right]))
def extract (content):
content = remove_empty_line(remove_js_css(content))
left,right,x,y = method_1 (content)
return '\n'.join(content.split('\n')[left:right])
代码 从最后一个函数开始调用。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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 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.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

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.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
