简体   繁体   中英

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.

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`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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