简体   繁体   English

用美丽的汤提取href

[英]Extracting href with Beautiful Soup

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

Link have no ID or class or whatever, it's just a classic link with a href attribute. 链接没有ID或类或其他什么,它只是一个带有href属性的经典链接。

The response of my script is : 我的脚本的响应是:

print link['href']
TypeError: string indices must be integers

Can you help me to get href value ? 你能帮助我获得href价值吗? Thx ! 谢谢 !

Links is still referring to your soup.find. 链接仍指你的汤。发现。 So you could do something like: 所以你可以这样做:

links = soup.find("span", { "class" : "hsmall" }).findNextSiblings('a')
for link in links:
    print link['href']
    print link.string

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`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM