Guest User

Get Title From URL Python Script (Google App Engine.)

a guest
Mar 12th, 2010
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. #Import CGI stuff.
  4. import cgi
  5. import cgitb
  6. cgitb.enable()
  7.  
  8. #Define terms.
  9. fail = "False"
  10. site_title = "not Set alskdfjqwerjqewroiq"
  11.  
  12. #Get arguments from URL. We only need the URL and the Title, so that's all we'll check for.
  13. f = cgi.FieldStorage()
  14. for i in f.keys():
  15.     if i == "url":
  16.         site_url = f[i].value
  17.     if i == "title":
  18.         site_title = f[i].value
  19.  
  20. #Check if we have the title. This will fail if somebody titled their webpage with the words we check for. But that's highly improbable.
  21. if site_title == "not Set alskdfjqwerjqewroiq":
  22.     fail = "true"
  23. else:
  24.     print
  25.     print "Good to go."
  26.  
  27.  
  28. #On the first run, we will not have the title, so we need to get it.   
  29. if fail == "true":
  30. #   Import stuff.
  31.     import urllib2
  32.     import BeautifulSoup
  33.     import re
  34.  
  35. #   Read the website.  
  36.     p = urllib2.urlopen(site_url).read()
  37. #   I don't know what this does. >.< But it works. (I know: bad coding. :( )
  38.     s = BeautifulSoup.BeautifulSoup(p)
  39.  
  40.    
  41. #   By printing out the webpage we can execute Javascript in it.
  42.     print s
  43.  
  44. #   Execute Javascript in the page that will open up this script with both a URL and Title .
  45.     print '<SCRIPT LANGUAGE = JavaScript>\nwindow.location = ("http://oisaac-deletepost.appspot.com/new_post.py?url=' + site_url + '&title=" + document.title)\n</SCRIPT>'
Add Comment
Please, Sign In to add comment