开发者

Extracting href with Beautiful Soup

开发者 https://www.devze.com 2023-03-30 06:59 出处:网络
I use this code to get acces to my link : links = soup.find(\"span\", { \"class\" : \"hsmall\" }) links.findNextSiblings(\'a\')

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.

The response of my script is :

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

Can you help me to get href value ? 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`
0

精彩评论

暂无评论...
验证码 换一张
取 消