美丽的汤儿童标签在提取后留下

时间:2013-05-15 00:09:04

标签: python tags beautifulsoup

在提取我不希望列表理解的标签时,仍然有一些标签应该被删除。

import requests, pprint
from bs4 import BeautifulSoup as bs

blacklist = ['a', 'title', 'p', 'input', 'u', 'body', 'html',
         'textarea', 'nobr', 'b', 'span', 'td', 'tr', 
         'br', 'table', 'form', 'img', 'head', 'meta', 
         'script', 'style', 'center',]

soup = bs(requests.get('http://www.google.com').text)

soup = [s.extract() for s in soup() if s.name not in blacklist]

# when printing the tag names, the only show tag is div.
# pprint.pprint( [s.name for s in soup] )

# inside of the divs are tags that we don't want.
pprint.pprint(soup)

输出

[<div id="mngb"></div>,
 <div id="gbar"><nobr><b class="gb1">Search</b> <a class="gb1" href="http://www.google.com/imghp?hl=en&amp;tab=wi">Images</a> <a class="gb1" href="http://maps.google.com/maps?hl=en&amp;tab=wl">Maps</a> <a class="gb1" href="https://play.google.com/?hl=en&amp;tab=w8">Play</a> <a class="gb1" href="http://www.youtube.com/?tab=w1">YouTube</a> <a class="gb1" href="http://news.google.com/nwshp?hl=en&amp;tab=wn">News</a> <a class="gb1" href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class="gb1" href="https://drive.google.com/?tab=wo">Drive</a> <a class="gb1" href="http://www.google.com/intl/en/options/" style="text-decoration:none"><u>More</u> »</a></nobr></div>,
 <div id="guser" width="100%"><nobr><span class="gbi" id="gbn"></span><span class="gbf" id="gbf"></span><span id="gbe"></span><a class="gb4" href="http://www.google.com/history/optout?hl=en">Web History</a> | <a class="gb4" href="/preferences?hl=en">Settings</a> | <a class="gb4" href="https://accounts.google.com/ServiceLogin?hl=en&amp;continue=http://www.google.com/" id="gb_70" target="_top">Sign in</a></nobr></div>,
 <div class="gbh" style="left:0"></div>,
 <div class="gbh" style="right:0"></div>,
 <div id="lga"><img alt="Google" height="95" id="hplogo" onload="window.lol&amp;&amp;lol()" src="/intl/en_ALL/images/srpr/logo1w.png" style="padding:28px 0 14px" width="275"/><br/><br/></div>,
 <div class="ds" style="height:32px;margin:4px 0"><input autocomplete="off" class="lst" maxlength="2048" name="q" size="57" style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top" title="Google Search" value=""/></div>,
 <div id="gac_scont"></div>,
 <div style="font-size:83%;min-height:3.5em"><br/></div>,
 <div style="font-size:10pt"></div>,
 <div id="fll" style="margin:19px auto;text-align:center"><a href="/intl/en/ads/">Advertising Programs</a><a href="/services/">Business Solutions</a><a href="https://plus.google.com/116899029375914044550" rel="publisher">+Google</a><a href="/intl/en/about.html">About Google</a></div>,
 <div id="xjsd"></div>,
 <div id="xjsi"><script>if(google.y)google.y.first=[];(function(){function b(a){window.setTimeout(function(){var c=document.createElement("script");c.src=a;document.getElementById("xjsd").appendChild(c)},0)}google.dljp=function(a){google.xjsi||(google.xjsu=a,b(a))};google.dlj=b;})();
if(!google.xjs){google.dstr=[];google.rein=[];window._=window._||{};window._._DumpException=function(e){throw e};if(google.timers&amp;&amp;google.timers.load.t){google.timers.load.t.xjsls=new Date().getTime();}google.dljp('/xjs/_/js/k\x3dPxufaYa-26A.en_US./m\x3dsb_he,pcc/rt\x3dj/d\x3d1/sv\x3d1/rs\x3dAItRSTNuFuVo3tYsbamkH3IQObWPur6JEA');google.xjs=1;}google.pmc={"sb":{"agen":true,"cgen":true,"client":"heirloom-hp","dh":true,"ds":"","eqch":true,"fl":true,"host":"google.com","jsonp":true,"msgs":{"lcky":"I\u0026#39;m Feeling Lucky","lml":"Learn more","oskt":"Input tools","psrc":"This search was removed from your \u003Ca href=\"/history\"\u003EWeb History\u003C/a\u003E","psrl":"Remove","sbit":"Search by image","srch":"Google Search"},"ovr":{"l":1,"ms":1},"pq":"","qcpw":false,"scd":10,"sce":5,"stok":"btuwXqiMkjlVCutQ1U6PC2HrVdE"},"hp":{},"pcc":{}};google.y.first.push(function(){if(google.med){google.med('init');google.initHistory();google.med('history');}google.History&amp;&amp;google.History.initialize('/');google.hs&amp;&amp;google.hs.init&amp;&amp;google.hs.init()});if(google.j&amp;&amp;google.j.en&amp;&amp;google.j.xi){window.setTimeout(google.j.xi,0);}</script></div>]

如何删除我不想要的标签我想要的标签?更具体地说,我需要用于所有情况的方法,这段代码只是一个简单的例子。

3 个答案:

答案 0 :(得分:0)

试试这个:

blacklist = ['a', 'title', 'p', 'input', 'u', 'body', 'html','textarea', 'nobr', 'b', 'span', 'td', 'tr', 'br', 'table', 'form', 'img', 'head', 'meta', 'script', 'style', 'center']
soup = [tag for tag in soup.findAll(True) if tag.name not in blacklist]

答案 1 :(得分:0)

由于我理解你想要的结果,我认为你应该通过文本方式(字面意思)迭代html doc来删除标签,而不是通过标签和使用BS对象。

如果对象被视为树,我的意思是分层次地如何处理以下情况?

你想要删除每个标签'a'而不是'div',文档有这样的树路径:

<a>
   <div>
       <a>
           <div>
                text
           </div>
       </a>
   </div>
</a>

如果删除最顶层节点'a',您还会删除所有经常出现的子节点。 如果您将文档分析为文本可能应该删除字面上的所有字符串'&lt; a&gt;'和'&lt; / a&gt;' (在那个例子中)。所以你应该使用正则表达式来管理它。

答案 2 :(得分:0)

玩了这么久后,我能够提取儿童标签。你需要找到孩子然后提取标签。

继承代码

import requests, pprint
from bs4 import BeautifulSoup as bs

blacklist = ['a', 'title', 'p', 'input', 'u', 'body', 'html',
         'textarea', 'nobr', 'b', 'span', 'td', 'tr', 
         'br', 'table', 'form', 'img', 'head', 'meta', 
         'script', 'style', 'center',]

soup = bs(requests.get('http://www.google.com').text)

# remove all blacklisted tags 
tags = [tag for tag in soup.find_all(True) if tag.name not in blacklist]

# show tag tree after first extraction
tag_tree = [(tag.name, [t.name for t in tag.findChildren()]) for tag in tags ]
for tree in tag_tree: print tree


# remove children tags that are blacklisted
for tag in tags:
    for child in tag.findChildren():
        if child.name in blacklist:
            child.extract()

pprint.pprint(tags)