FamiHug

load_vital.py

Jul 12th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. from urllib2 import Request, urlopen
  2. from BeautifulSoup import BeautifulSoup, SoupStrainer
  3.  
  4. dest =  "http://tratu.soha.vn/"
  5. dest = "http://vnexpress.net/"
  6. dest = "http://vnexpress.net/gl/the-gioi/"
  7. dest = "https://www.facebook.com/"
  8. dest = "http://kenh14.vn/"
  9. request  = Request(dest)
  10. response = urlopen(request)
  11.  
  12. vital_comps = []
  13.  
  14. def remove_prefix_slash(string):
  15.     """Must do this because we trail all link with /"""
  16.     try:
  17.         if string[0] == '/':
  18.             return string[1:]
  19.         else:
  20.             return string
  21.     except:
  22.         return string
  23.  
  24. def is_link_alive(link):
  25.     try:
  26.         urlopen(link)
  27.         return True
  28.     except:
  29.         return False
  30.  
  31. def find_tag_and_check_link(soup, url, tag, before_link):
  32.     #TODO trim url
  33.     for comp in soup.findAll(tag):
  34.         if comp.has_key(before_link):
  35.             string = ''.join(comp[before_link])
  36.             string = remove_prefix_slash(string)
  37.  
  38.             if string[:7] == "http://" or string[:8] == "https://":
  39.                 #external link
  40.                 full_link = string
  41.             else:
  42.                 #internal link
  43.                 full_link = url + string
  44.             print full_link, is_link_alive(full_link)
  45.  
  46.  
  47. soup3 = BeautifulSoup(response)
  48. #find all js
  49. find_tag_and_check_link(soup3, dest, 'script', 'src')
  50.  
  51. #find all css
  52. find_tag_and_check_link(soup3, dest, 'link', 'href')
Advertisement
Add Comment
Please, Sign In to add comment