【问题标题】:Extracting link title from beautiful soup python从美丽的汤蟒中提取链接标题
【发布时间】:2015-06-09 19:33:00
【问题描述】:

假设我的文本是

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

我想在&lt;a&gt; and &lt;/a&gt;标签之间找到文本

    <a href = "link">This part here</a>

另外,我需要确保“This part here”不包含任何标签。

我正在做的是,

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'

【问题讨论】:

    标签: python html beautifulsoup html-parsing nonetype


    【解决方案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
    

    【讨论】:

    【解决方案2】:

    使用text而不是string,即改变

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2019-11-07
      • 2012-07-16
      相关资源
      最近更新 更多