如何用Python实现网页正文的提取-创新互联

这篇文章主要介绍了如何用Python实现网页正文的提取的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何用Python实现网页正文的提取文章都会有所收获,下面我们一起来看看吧。

创新互联建站长期为上千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为船营企业提供专业的网站建设、成都网站设计船营网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

一个典型的新闻网页包括几个不同区域:

如何用Python实现网页正文的提取

新闻网页区域

我们要提取的新闻要素包含在:

  • 标题区域

  • meta数据区域(发布时间等)

  • 配图区域(如果想把配图也提取)

  • 正文区域

而导航栏区域、相关链接区域的文字就不属于该新闻的要素。

新闻的标题、发布时间、正文内容一般都是从我们抓取的html里面提取的。如果仅仅是一个网站的新闻网页,提取这三个内容很简单,写三个正则表达式就可以完美提取了。然而,我们的爬虫抓来的是成百上千的网站的网页。对这么多不同格式的网页写正则表达式会累死人的,而且网页一旦稍微改版,表达式可能就失效,维护这群表达式也是会累死人的。

累死人的做法当然想不通,我们就要探索一下好的算法来实现。

1. 标题的提取

标题基本上都会出现在html的</code>标签里面,但是又被附加了诸如频道名称、网站名称等信息;</p><p>标题还会出现在网页的“标题区域”。</p><p>那么这两个地方,从哪里提取标题比较容易呢?</p><p>网页的“标题区域”没有明显的标识,不同网站的“标题区域”的html代码部分千差万别。所以这个区域并不容易提取出来。</p><p>那么就只剩下<code><title></code>标签了,这个标签很容易提取,无论是正则表达式,还是lxml解析都很容易,不容易的是如何去除频道名称、网站名称等信息。</p><p>先来看看,<code><title></code>标签里面都是设么样子的附加信息:</p><ul><li><p><code>上海用“智慧”激活城市交通脉搏,让道路更安全更有序更通畅_浦江头条_澎湃新闻-The Paper</code></p></li><li><p><code>“沪港大学联盟”今天在复旦大学成立_教育_新民网</code></p></li><li><p><code>三亚老人脚踹司机致公交车失控撞墙 被判刑3年_社会</code></p></li><li><p><code>外交部:中美外交安全对话9日在美举行</code></p></li><li><p><code>进博会:中国行动全球瞩目,中国担当世界点赞_南方观澜_南方网</code></p></li><li><p><code>资本市场迎来重大改革 设立科创板有何深意?-新华网</code></p></li></ul><p>观察这些title不难发现,新闻标题和频道名、网站名之间都是有一些连接符号的。那么我就可以通过这些连接符吧title分割,找出最长的部分就是新闻标题了。</p><p>这个思路也很容易实现,这里就不再上代码了,留给小猿们作为思考练习题自己实现一下。</p><h3>2. 发布时间提取</h3><p>发布时间,指的是这个网页在该网站上线的时间,一般它会出现在正文标题的下方——meta数据区域。从html代码看,这个区域没有什么特殊特征让我们定位,尤其是在非常多的网站版面面前,定位这个区域几乎是不可能的。这需要我们另辟蹊径。<br/>跟标题一样,我们也先看看一些网站的发布时间都是怎么写的:</p><ul><li><p>央视网2018年11月06日 22:22</p></li><li><p>时间:2018-11-07 14:27:00</p></li><li><p>2018-11-07 11:20:37 来源: 新华网</p></li><li><p>来源:中国日报网 2018-11-07 08:06:39</p></li><li><p>2018年11月07日 07:39:19</p></li><li><p>2018-11-06 09:58 来源:澎湃新闻</p></li></ul><p>这些写在网页上的发布时间,都有一个共同的特点,那就是一个表示时间的字符串,年月日时分秒,无外乎这几个要素。通过正则表达式,我们列举一些不同时间表达方式(也就那么几种)的正则表达式,就可以从网页文本中进行匹配提取发布时间了。</p><p>这也是一个很容易实现的思路,但是细节比较多,表达方式要涵盖的尽可能多,写好这么一个提取发布时间的函数也不是那么容易的哦。小猿们尽情发挥动手能力,看看自己能写出怎样的函数实现。这也是留给小猿们的一道练习题。</p><h3>3. 正文的提取</h3><p>正文(包括新闻配图)是一个新闻网页的主体部分,它在视觉上占据中间位置,是新闻的内容主要的文字区域。正文的提取有很多种方法,实现上有复杂也有简单。本文介绍的方法,是结合老猿多年的实践经验和思考得出来的一个简单快速的方法,姑且称之为“节点文本密度法”。</p><p>我们知道,网页的html代码是由不同的标签(tag)组成了一个树状结构树,每个标签是树的一个节点。通过遍历这个树状结构的每个节点,找到文本最多的节点,它就是正文所在的节点。根据这个思路,我们来实现一下代码。</p><h4>3.1 实现源码</h4><pre>#!/usr/bin/env python3 #File: maincontent.py #Author: veelion import re import time import traceback import cchardet import lxml import lxml.html from lxml.html import HtmlComment REGEXES = {     'okMaybeItsACandidateRe': re.compile(         'and|article|artical|body|column|main|shadow', re.I),     'positiveRe': re.compile(         ('article|arti|body|content|entry|hentry|main|page|'          'artical|zoom|arti|context|message|editor|'          'pagination|post|txt|text|blog|story'), re.I),     'negativeRe': re.compile(         ('copyright|combx|comment|com-|contact|foot|footer|footnote|decl|copy|'          'notice|'          'masthead|media|meta|outbrain|promo|related|scroll|link|pagebottom|bottom|'          'other|shoutbox|sidebar|sponsor|shopping|tags|tool|widget'), re.I), } class MainContent:     def __init__(self,):         self.non_content_tag = set([             'head',             'meta',             'script',             'style',             'object', 'embed',             'iframe',             'marquee',             'select',         ])         self.title = ''         self.p_space = re.compile(r'\s')         self.p_html = re.compile(r'<html|</html>', re.IGNORECASE|re.DOTALL)         self.p_content_stop = re.compile(r'正文.*结束|正文下|相关阅读|声明')         self.p_clean_tree = re.compile(r'author|post-add|copyright')     def get_title(self, doc):         title = ''         title_el = doc.xpath('//title')         if title_el:             title = title_el[0].text_content().strip()         if len(title) < 7:             tt = doc.xpath('//meta[@name="title"]')             if tt:                 title = tt[0].get('content', '')         if len(title) < 7:             tt = doc.xpath('//*[contains(@id, "title") or contains(@class, "title")]')             if not tt:                 tt =  doc.xpath('//*[contains(@id, "font01") or contains(@class, "font01")]')             for t in tt:                 ti = t.text_content().strip()                 if ti in title and len(ti)*2 > len(title):                     title = ti                     break                 if len(ti) > 20: continue                 if len(ti) > len(title) or len(ti) > 7:                     title = ti         return title     def shorten_title(self, title):         spliters = [' - ', '–', '—', '-', '|', '::']         for s in spliters:             if s not in title:                 continue             tts = title.split(s)             if len(tts) < 2:                 continue             title = tts[0]             break         return title     def calc_node_weight(self, node):         weight = 1         attr = '%s %s %s' % (             node.get('class', ''),             node.get('id', ''),             node.get('style', '')         )         if attr:             mm = REGEXES['negativeRe'].findall(attr)             weight -= 2 * len(mm)             mm = REGEXES['positiveRe'].findall(attr)             weight += 4 * len(mm)         if node.tag in ['div', 'p', 'table']:             weight += 2         return weight     def get_main_block(self, url, html, short_title=True):         ''' return (title, etree_of_main_content_block)         '''         if isinstance(html, bytes):             encoding = cchardet.detect(html)['encoding']             if encoding is None:                 return None, None             html = html.decode(encoding, 'ignore')         try:             doc = lxml.html.fromstring(html)             doc.make_links_absolute(base_url=url)         except :             traceback.print_exc()             return None, None         self.title = self.get_title(doc)         if short_title:             self.title = self.shorten_title(self.title)         body = doc.xpath('//body')         if not body:             return self.title, None         candidates = []         nodes = body[0].getchildren()         while nodes:             node = nodes.pop(0)             children = node.getchildren()             tlen = 0             for child in children:                 if isinstance(child, HtmlComment):                     continue                 if child.tag in self.non_content_tag:                     continue                 if child.tag == 'a':                     continue                 if child.tag == 'textarea':                     # FIXME: this tag is only part of content?                     continue                 attr = '%s%s%s' % (child.get('class', ''),                                    child.get('id', ''),                                    child.get('style'))                 if 'display' in attr and 'none' in attr:                     continue                 nodes.append(child)                 if child.tag == 'p':                     weight = 3                 else:                     weight = 1                 text = '' if not child.text else child.text.strip()                 tail = '' if not child.tail else child.tail.strip()                 tlen += (len(text) + len(tail)) * weight             if tlen < 10:                 continue             weight = self.calc_node_weight(node)             candidates.append((node, tlen*weight))         if not candidates:             return self.title, None         candidates.sort(key=lambda a: a[1], reverse=True)         good = candidates[0][0]         if good.tag in ['p', 'pre', 'code', 'blockquote']:             for i in range(5):                 good = good.getparent()                 if good.tag == 'div':                     break         good = self.clean_etree(good, url)         return self.title, good     def clean_etree(self, tree, url=''):         to_drop = []         drop_left = False         for node in tree.iterdescendants():             if drop_left:                 to_drop.append(node)                 continue             if isinstance(node, HtmlComment):                 to_drop.append(node)                 if self.p_content_stop.search(node.text):                     drop_left = True                 continue             if node.tag in self.non_content_tag:                 to_drop.append(node)                 continue             attr = '%s %s' % (                 node.get('class', ''),                 node.get('id', '')             )             if self.p_clean_tree.search(attr):                 to_drop.append(node)                 continue             aa = node.xpath('.//a')             if aa:                 text_node = len(self.p_space.sub('', node.text_content()))                 text_aa = 0                 for a in aa:                     alen = len(self.p_space.sub('', a.text_content()))                     if alen > 5:                         text_aa += alen                 if text_aa > text_node * 0.4:                     to_drop.append(node)         for node in to_drop:             try:                 node.drop_tree()             except:                 pass         return tree     def get_text(self, doc):         lxml.etree.strip_elements(doc, 'script')         lxml.etree.strip_elements(doc, 'style')         for ch in doc.iterdescendants():             if not isinstance(ch.tag, str):                 continue             if ch.tag in ['div', 'h2', 'h3', 'h4', 'p', 'br', 'table', 'tr', 'dl']:                 if not ch.tail:                     ch.tail = '\n'                 else:                     ch.tail = '\n' + ch.tail.strip() + '\n'             if ch.tag in ['th', 'td']:                 if not ch.text:                     ch.text = '  '                 else:                     ch.text += '  '             # if ch.tail:             #     ch.tail = ch.tail.strip()         lines = doc.text_content().split('\n')         content = []         for l in lines:             l = l.strip()             if not l:                 continue             content.append(l)         return '\n'.join(content)     def extract(self, url, html):         '''return (title, content)         '''         title, node = self.get_main_block(url, html)         if node is None:             print('\tno main block got !!!!!', url)             return title, '', ''         content = self.get_text(node)         return title, content</pre><h4>3.2 代码解析</h4><p>跟新闻爬虫一样,我们把整个算法实现为一个类:MainContent。</p><p>首先,定义了一个全局变量: REGEXES。它收集了一些经常出现在标签的class和id中的关键词,这些词标识着该标签可能是正文或者不是。我们用这些词来给标签节点计算权重,也就是方法calc_node_weight()的作用。</p><p>MainContent类的初始化,先定义了一些不会包含正文的标签 self.non_content_tag,遇到这些标签节点,直接忽略掉即可。</p><p>本算法提取标题实现在get_title()这个函数里面。首先,它先获得<code><title></code>标签的内容,然后试着从<code><meta></code>里面找title,再尝试从<code><body></code>里面找id和class包含title的节点,最后把从不同地方获得的可能是标题的文本进行对比,最终获得标题。对比的原则是:</p><ul><li><p><code><meta></code>, <code><body></code>里面找到的疑似标题如果包含在<code><title></code>标签里面,则它是一个干净(没有频道名、网站名)的标题;</p></li><li><p>如果疑似标题太长就忽略</p></li><li><p>主要把<code><title></code>标签作为标题</p></li></ul><p>从<code><title></code>标签里面获得标题,就要解决标题清洗的问题。这里实现了一个简单的方法: clean_title()。</p><p>在这个实现中,我们使用了lxml.html把网页的html转化成一棵树,从body节点开始遍历每一个节点,看它直接包含(不含子节点)的文本的长度,从中找出含有最长文本的节点。这个过程实现在方法:get_main_block()中。其中一些细节,小猿们可以仔细体会一下。</p><p>其中一个细节就是,clean_node()这个函数。通过get_main_block()得到的节点,有可能包含相关新闻的链接,这些链接包含大量新闻标题,如果不去除,就会给新闻内容带来杂质(相关新闻的标题、概述等)。</p><p>还有一个细节,get_text()函数。我们从main block中提取文本内容,不是直接使用text_content(),而是做了一些格式方面的处理,比如在一些标签后面加入换行符合<code>\n</code>,在table的单元格之间加入空格。这样处理后,得到的文本格式比较符合原始网页的效果。</p><h3>爬虫知识点</h3><p>1. cchardet模块<br/>用于快速判断文本编码的模块</p><p>2. lxml.html模块<br/>结构化html代码的模块,通过xpath解析网页的工具,高效易用,是写爬虫的居家必备的模块。</p><p>3. 内容提取的复杂性<br/>我们这里实现的正文提取的算法,基本上可以正确处理90%以上的新闻网页。<br/>但是,世界上没有千篇一律的网页一样,也没有一劳永逸的提取算法。大规模使用本文算法的过程中,你会碰到奇葩的网页,这个时候,你就要针对这些网页,来完善这个算法类。</p><p>关于“如何用Python实现网页正文的提取”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“如何用Python实现网页正文的提取”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联-成都网站建设公司行业资讯频道。</p> <br> 网页名称:如何用Python实现网页正文的提取-创新互联 <br> 标题网址:<a href="http://csdahua.cn/article/dccojj.html">http://csdahua.cn/article/dccojj.html</a> </div> <div class="view-qrocde cl"> <div class="m z"><img src="/Public/Home/images/ew.jpg"/></div> <div class="text"> <h6>扫二维码与项目经理沟通</h6> <p>我们在微信上24小时期待你的声音</p> <p>解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流</p> </div> </div> <div class="othernews cl"> <h3>其他资讯</h3> <ul> <li><a href="/article/deiejcs.html">最早的路由器 最早的路由器什么牌子</a></li><li><a href="/article/deiejie.html">linux复制粘贴命令行 linux 粘贴复制</a></li><li><a href="/article/deiejih.html">接两台无线路由器 两台路由器怎么有线连接无线</a></li><li><a href="/article/deiejgp.html">阿里云学生服务器续用考试 阿里云学生服务器续费考试</a></li><li><a href="/article/deiejeh.html">c语言汉诺塔函数sqrt的简单介绍</a></li> </ul> </div> </div> </div> <div class="sidebar"> <div class="tuijian"> <a href="#"> <h2 class="cl"><span>行业动态</span></h2> <h3>企业网站建设的重要性!</h3> <p>现在虽然是移动互联网时代,但企业网站依然重要,包含PC站点,移动站。可以说企业网站关系企业的未来发展和前途,尤其对中小企业更是如此,一些中小企业老板,对自己的名片很在乎,因为这是个门面。...</p> </a> </div> <div class="ser sidesub"> <h2>服务项目</h2> <ul class="ebox"> <li class="sub sub-1"> <div> <h3>网站建设</h3> <p></p> <a class="btn" href="/serve/website/">查看详情</a> </div> </li> <li class="sub sub-1"> <div> <h3>移动端/APP</h3> <p></p> <a class="btn" href="/serve/moblie/">查看详情</a> </div> </li> <li class="sub sub-1"> <div> <h3>微信/小程序</h3> <p></p> <a class="btn" href="/serve/small/">查看详情</a> </div> </li> <li class="sub sub-1"> <div> <h3>技术支持</h3> <p></p> <a class="btn" href="/serve/tech/">查看详情</a> </div> </li> <li class="sub sub-1"> <div> <h3>其它服务</h3> <p></p> <a class="btn" href="/serve/othe/">查看详情</a> </div> </li> <li class="sub sub-5"> <div> <h3>更多服务项目</h3> <p> <a>用我们的专业和诚信赢得您的信赖,从PC到移动互联网均有您想要的服务!</a></p> <a class="btn" href="/serve/">获取更多</a> </div> </li> </ul> </div> <div class="contact" id="fix"> <h2 class="cl"> <span>联系吧</span> <a href="https://map.baidu.com/" class="ditu" rel="nofollow" target="_blank">在百度地图上找到我们</a> </h2> <h3>电话:13518219792</h3> <p>如遇占线或暂未接听请拨:136xxx98888</p> <div class="qq"> <a href="//wpa.qq.com/msgrd?v=3&uin=244261566&site=qq&menu=yes" rel="nofollow" target="_blank">业务咨询</a> <a href="//wpa.qq.com/msgrd?v=3&uin=244261566&site=qq&menu=yes" rel="nofollow" target="_blank">技术咨询</a> <a href="//wpa.qq.com/msgrd?v=3&uin=244261566&site=qq&menu=yes" rel="nofollow" target="_blank">售后服务</a> </div> </div> </div> <script> //固定滚动 (function () { var oDiv = document.getElementById("fix"); var H = 120, iE6; var Y = oDiv; while (Y) { H += Y.offsetTop; Y = Y.offsetParent }; iE6 = window.ActiveXObject && !window.XMLHttpRequest; if (!iE6) { window.onscroll = function () { var s = document.body.scrollTop || document.documentElement.scrollTop; if (s > H) { oDiv.className = "contact fixed"; if (iE6) { oDiv.style.top = (s - H) + "px"; } } else { oDiv.className = "contact "; } }; } })(); </script> </div> <div class="footer"> <div class="wp"> <div class="wpss cl"> <dl class="about"> <dt>网站制作</dt> <dd><a href="http://chengdu.kswjz.com/" target="_blank" title="成都网站制作">成都网站制作</a></dd><dd><a href="http://www.cdxtjz.com/" target="_blank" title="网站制作">网站制作</a></dd><dd><a href="http://www.kswsj.com/" target="_blank" title="成都网站制作">成都网站制作</a></dd><dd><a href="http://www.wjzwz.com/" target="_blank" title="温江网站制作">温江网站制作</a></dd> </dl> <dl class="about"> <dt>网站建设</dt> <dd><a href="http://www.kswcd.cn/" target="_blank" title="专业网站建设">专业网站建设</a></dd><dd><a href="https://www.cdcxhl.com/" target="_blank" title="网站建设">网站建设</a></dd><dd><a href="http://www.myzwz.com/" target="_blank" title="绵阳网站建设公司">绵阳网站建设公司</a></dd><dd><a href="https://www.cdcxhl.com/h5.html" target="_blank" title="成都h5网站建设">成都h5网站建设</a></dd> </dl> <dl class="about"> <dt>网站设计</dt> <dd><a href="http://www.gawzjs.com/" target="_blank" title="广安网站设计">广安网站设计</a></dd><dd><a href="http://www.cdxwcx.cn/sheji/" target="_blank" title="成都网站设计">成都网站设计</a></dd><dd><a href="http://www.kswcd.cn/serve/" target="_blank" title="高端网站设计">高端网站设计</a></dd><dd><a href="https://www.cdcxhl.com/" target="_blank" title="网站设计">网站设计</a></dd> </dl> <dl class="contact"> <dt>联系我们</dt> <dd>电话:13518219792</dd> <dd>邮箱:631063699@qq.com</dd> <dd>地址:成都青羊区锦天国际1002号</dd> <dd>网址:www.csdahua.cn</dd> </dl> <dl class="flow"> <dt></dt> <div class="ma cl"> <div class="m"> <img src="/Public/Home/images/ew.jpg" /> <p>微信二维码</p> </div> </div> </dl> </div> </div> <div class="footer-link wp"> <ul class="wpss cl"> <li class="fisrt">友情链接</li> <li><a href="http://chengdu.cdweb.net/weixinkaifa/qiyeweixin.html" title="企业微信定制" target="_blank">企业微信定制</a></li><li><a href="https://www.cdcxhl.com/xiangyingshi.html" title="响应式网站建设" target="_blank">响应式网站建设</a></li><li><a href="http://chengdu.cdcxhl.cn/seo/" title="营销网站建设" target="_blank">营销网站建设</a></li><li><a href="http://m.cdcxhl.cn/wechat/ " title="微信公众号开发" target="_blank">微信公众号开发</a></li><li><a href="http://www.cdhuace.com/zhuangxiu.html" title="成都店面装修公司" target="_blank">成都店面装修公司</a></li><li><a href="http://www.jxjierui.cn/" title="内江智速网站建设" target="_blank">内江智速网站建设</a></li><li><a href="https://www.cdcxhl.com/app.html" title="成都app开发" target="_blank">成都app开发</a></li><li><a href="http://www.33260.cn/" title="成都定制网站建设" target="_blank">成都定制网站建设</a></li><li><a href="https://www.cdxwcx.com/wangzhan/app.html" title="移动APP" target="_blank">移动APP</a></li><li><a href="http://www.hitbtccn.com/" title="草坪音箱" target="_blank">草坪音箱</a></li> </ul> </div> </div> <div class="bot-footer"> <div class="wp"> <p class="wpss"> <em>Copyright © 2002-2023 www.csdahua.cn 快上网建站品牌 QQ:244261566 版权所有</em> <em>备案号:<a href="http://beian.miit.gov.cn/" rel="external nofollow">蜀ICP备19037934号</a></em> </p> <p class="wpss" style="line-height:30px !important;"> </p> </div> </div> <div class="footer-kefu"> <ul> <li class="qq"><a href="https://wpa.qq.com/msgrd?v=3&uin=244261566&site=qq&menu=yes"><em></em>在线咨询</a> </li> <li class="tel"><a href="tel:13518219792" target="_blank"><em></em>13518219792</a></li> <li class="wx"> <em></em> <div class="code"> <img src="/Public/Home/images/ew.jpg" /> <p>微信二维码</p> </div> </li> <li class="m"> <em></em> <div class="code"> <img src="/Public/Home/images/ew.jpg" /> <p>移动版官网</p> </div> </li> <li class="top"><em></em></li> </ul> </div> <script src="/Public/Home/js/all.js"></script> </body> </html> <script> $(".cont img").each(function(){ var src = $(this).attr("src"); //获取图片地址 var str=new RegExp("http"); var result=str.test(src); if(result==false){ var url = "https://www.cdcxhl.com"+src; //绝对路径 $(this).attr("src",url); } }); window.onload=function(){ document.oncontextmenu=function(){ return false; } } </script>