用 Beautiful Soup 提取 href [英] Extracting href with Beautiful Soup
本文介绍了用 Beautiful Soup 提取 href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此代码访问我的链接:
I use this code to get acces to my link :
links = soup.find("span", { "class" : "hsmall" })
links.findNextSiblings('a')
for link in links:
print link['href']
print link.string
链接没有 ID 或类或其他什么,它只是一个带有 href 属性的经典链接.
Link have no ID or class or whatever, it's just a classic link with a href attribute.
我的脚本的响应是:
print link['href']
TypeError: string indices must be integers
你能帮我获取 href 值吗?谢谢!
Can you help me to get href value ? Thx !
推荐答案
好的,现在可以使用以下代码:
Okay, it works now with following code :
linkSpan = soup.find("span", { "class" : "hsmall" })
link = [tag.attrMap['href'] for tag in linkSpan.findAll('a', {'href': True})]
for lien in link:
print "LINK = " + lien`
这篇关于用 Beautiful Soup 提取 href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文