用美丽的汤提取href

Koo*_*len 6 python beautifulsoup href

我使用此代码来访问我的链接:

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

链接没有ID或类或其他什么,它只是一个带有href属性的经典链接.

我的脚本的响应是:

print link['href']
TypeError: string indices must be integers
Run Code Online (Sandbox Code Playgroud)

你能帮助我获得href价值吗?谢谢 !

Chr*_*ett 9

链接仍指你的汤.发现.所以你可以这样做:

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


Koo*_*len 4

好的,现在可以使用以下代码:

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`
Run Code Online (Sandbox Code Playgroud)