假设我的文字是
<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 & 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&d=4967534815872005&mkt=en-US&setlang=en-US&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&n=30&ei=UTF-8&u=http://cc.bingj.com/cache.aspx?q=jaguar&d=4828270487339026&mkt=en-US&setlang=en-US&w=e_F5McXDYJWTpIIntdbN8Ht0ewcqxYpP&icp=1&.intl=us&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&n=30&ei=UTF-8&u=http://cc.bingj.com/cache.aspx?q=jaguar&d=4721944299241952&mkt=en-US&setlang=en-US&w=CBJMmRdVmMVjSqhyMCFPd1BAaDRR4HV4&icp=1&.intl=us&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&n=30&ei=UTF-8&u=http://cc.bingj.com/cache.aspx?q=jaguar&d=4808277429977536&mkt=en-US&setlang=en-US&w=NHU5GwfHQLSKjSLbPsXxTxyvRv2FsgCF&icp=1&.intl=us&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 & 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'
答案 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')