code_junkie

Decomposing HTML to link text and target

Nov 12th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. <a href="urltxt" class="someclass" close="true">texttxt</a>
  2.  
  3. soup = BeautifulSoup.BeautifulSoup(urllib.urlopen(url))
  4.  
  5. links = soup.findAll('a')
  6.  
  7. for link in links:
  8. print "link content:", link.content," and attr:",link.attrs
  9.  
  10. *link content: None and attr: [(u'href', u'_redirectGeneric.asp?genericURL=/root /support.asp')]* ...
  11. ...
  12.  
  13. soup = BeautifulSoup.BeautifulSoup(urllib.urlopen(url).read())
  14.  
  15. import urlparse, urllib
  16. from BeautifulSoup import BeautifulSoup
  17.  
  18. url = "http://www.example.com/index.html"
  19. source = urllib.urlopen(url).read()
  20.  
  21. soup = BeautifulSoup(source)
  22.  
  23. for item in soup.fetchall('a'):
  24. try:
  25. link = urlparse.urlparse(item['href'].lower())
  26. except:
  27. # Not a valid link
  28. pass
  29. else:
  30. print link
  31.  
  32. soup = BeautifulSoup.BeautifulSoup(urllib.urlopen(url))
  33. for link in soup.findAll('a'):
  34. print link.attrs, link.contents
  35.  
  36. /<as+[^>]*?href="([^"]*)".*?>(.*?)</a>/
  37.  
  38. '<a href="url" close="true">text</a>'
  39. // Parts: "url", "text"
  40.  
  41. '<a href="url" close="true">text<span>something</span></a>'
  42. // Parts: "url", "text<span>something</span>"
Add Comment
Please, Sign In to add comment