从美丽的汤蟒蛇中提取链接标题

时间:2015-04-05 03:09:32

标签: python html beautifulsoup html-parsing nonetype

假设我的文字是

   <div class="compTitle"><h3 class="title"><a href="http://www.jaguarusa.com/index.html" target="_blank" data-80a="5520a3b52c6fe"><b></b><b>Jaguar</b>: Luxury Cars &amp; Sports Cars | <b></b><b>Jaguar</b> USA</a></h3> <div><span class=" fz-15px fw-m fc-12th wr-bw">www.<b></b><b>jaguar</b>usa.com</span><a class="tri fc-3rd fz-2xs ml-10" href="http://cc.bingj.com/cache.aspx?q=jaguar&amp;d=4967534815872005&amp;mkt=en-US&amp;setlang=en-US&amp;w=EK-x56coxxfS-VYEa33LnCSKRL4yj3aK" target="_blank" data-80a="5520a3b52c7ec">Cached</a></div></div>

   <div class="compTitle"><h3 class="title"><a class=" td-u" href="http://www.jaguar.com/index.html" target="_blank" data-80a="5520a3b52d37b"><b></b><b>Jaguar</b> - How Alive Are You?</a></h3> <div><span class=" fz-15px fw-m fc-12th wr-bw">www.<b></b><b>jaguar</b>.com/index.html</span><a class="tri fc-3rd fz-2xs ml-10" href="http://208.71.46.190/search/srpcache?p=jaguar&amp;n=30&amp;ei=UTF-8&amp;u=http://cc.bingj.com/cache.aspx?q=jaguar&amp;d=4828270487339026&amp;mkt=en-US&amp;setlang=en-US&amp;w=e_F5McXDYJWTpIIntdbN8Ht0ewcqxYpP&amp;icp=1&amp;.intl=us&amp;sig=HM1uWmpgoDFepAuhoyxPwA--" target="_blank" data-80a="5520a3b52d463">Cached</a></div></div>

   <div class="compTitle"><h3 class="title"><a class=" td-u" href="http://www.jaguar.com/market-selector.html" target="_blank" data-80a="5520a3b52d5a4"><b></b><b>Jaguar</b> International - Market selector page</a></h3> <div><span class=" fz-15px fw-m fc-12th wr-bw">www.<b></b><b>jaguar</b>.com</span><a class="tri fc-3rd fz-2xs ml-10" href="http://208.71.46.190/search/srpcache?p=jaguar&amp;n=30&amp;ei=UTF-8&amp;u=http://cc.bingj.com/cache.aspx?q=jaguar&amp;d=4721944299241952&amp;mkt=en-US&amp;setlang=en-US&amp;w=CBJMmRdVmMVjSqhyMCFPd1BAaDRR4HV4&amp;icp=1&amp;.intl=us&amp;sig=2oXq96OH3i158VrEx2GhPA--" target="_blank" data-80a="5520a3b52d68a">Cached</a></div></div>

   <div class="compTitle"><h3 class="title"><a class=" td-u" href="http://animals.nationalgeographic.com/animals/mammals/jaguar/" target="_blank" data-80a="5520a3b52d7c8"><b>Jaguars</b>, <b></b><b>Jaguar</b> Pictures, <b></b><b>Jaguar</b> Facts - National Geographic</a></h3> <div><span class=" fz-15px fw-m fc-12th wr-bw">animals.nationalgeographic.com/animals/mammals/<b></b><b>jaguar</b></span><a class="tri fc-3rd fz-2xs ml-10" href="http://208.71.46.190/search/srpcache?p=jaguar&amp;n=30&amp;ei=UTF-8&amp;u=http://cc.bingj.com/cache.aspx?q=jaguar&amp;d=4808277429977536&amp;mkt=en-US&amp;setlang=en-US&amp;w=NHU5GwfHQLSKjSLbPsXxTxyvRv2FsgCF&amp;icp=1&amp;.intl=us&amp;sig=OMQN5z2J_effVch27OsP9A--" target="_blank" data-80a="5520a3b52d8ad">Cached</a></div></div>

我希望找到<a> and </a>代码

之间的文字
    <a href = "link">This part here</a>

另外,我需要确保“此处此部分”不包含任何标记。

我在做的是,

headers = soup.findAll('div','compTitle')
for header in headers:
    if header is not None:
        t = header.a.string.encode('utf-8')
        print t

所以,我的预期输出是

Jaguar: Luxury Cars &amp; Sports Cars | Jaguar USA
Jaguar - How Alive Are You?
Jaguar International - Market selector page
Jaguars, Jaguar Pictures, Jaguar Facts - National Geographic

但我收到的错误是

AttributeError: 'NoneType' object has no attribute 'encode'

2 个答案:

答案 0 :(得分:1)

然后尝试:

>>> for x in soup.find_all('div', 'compTitle'):
...     print x.a.text
... 
Jaguar: Luxury Cars & Sports Cars | Jaguar USA
Jaguar - How Alive Are You?
Jaguar International - Market selector page
Jaguars, Jaguar Pictures, Jaguar Facts - National Geographic

答案 1 :(得分:1)

使用text代替string,即更改

t = header.a.string.encode('utf-8')

t = header.a.text.encode('utf-8')