gpt4 book ai didi

python - 从 BeautifulSoup Python中提取链接标题

转载 作者:行者123 更新时间:2023-12-01 04:45:41 27 4
gpt4 key购买 nike

假设我的文字是

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

最佳答案

然后尝试这样:

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

关于python - 从 BeautifulSoup Python中提取链接标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29453720/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com