User:Eleanorg/1.2/Forbidden Pixels/writing string to text file: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "This script recieves a url from a form, scrapes it, extracts relevant bits with regex, and writes relevant bits to a text file. <source lang="python"> #!/usr/bin/python #-*- cod...")
 
No edit summary
 
Line 37: Line 37:
   <body>"""
   <body>"""


 
print "Content-Type: text/html"
print
print htmlHeader


pat = r"(Pixel position:(\d\d\d).(\d\d\d)\;\ Color:(rgba\(.*\)))"
pat = r"(Pixel position:(\d\d\d).(\d\d\d)\;\ Color:(rgba\(.*\)))"
Line 46: Line 48:
     f = open("data/data.txt", 'a')  # opens text file in apend mode
     f = open("data/data.txt", 'a')  # opens text file in apend mode
     for m in re.finditer(pat, text): # for each match in text
     for m in re.finditer(pat, text): # for each match in text
        # print m                              # prints the match object from re.findinter()
         string = m.group(0)                    # m.group() lets you access data within the match object
         string = m.group(0)                    # m.group() lets you access data within the match object
xPos = m.group(1)
xPos = m.group(1)
Line 52: Line 53:
color = m.group(3)
color = m.group(3)
f.write(xPos + '\n') # writes xPos to the text file plus newline
f.write(xPos + '\n') # writes xPos to the text file plus newline
         print "thanks, url submitted. The pixel you are hosting has been added."
        print htmlHeader
         print """
        thanks, url submitted
        </body>
        </html>"""
     f.close()
     f.close()
   
print """
  </body>
</html>"""




</source>
</source>

Latest revision as of 19:39, 28 March 2012

This script recieves a url from a form, scrapes it, extracts relevant bits with regex, and writes relevant bits to a text file.

#!/usr/bin/python
#-*- coding:utf-8 -*-

import cgi, re, urllib2
import cgitb; cgitb.enable()

 
# scrapes pixel data string from a URL submitted by user in an html form; assigns the result to appropriate variables.
 

#------------- get URL from input form -------------------#

form = cgi.FieldStorage()		# Grabs whatever input comes from form
url = form.getvalue("url", "http://ox4.org/~nor/trials/hostedString.html")  # assigns form's input to var 'url'. url on the right is a default value that will be printed for testing if nothing is recieved from the form

#------------- scrape webpage----------------------------#

text = urllib2.urlopen(url).read()	# reads page at the specified URL

 
#--------- extract the string with regex; write it to text file------------#

# string is in format: 
# Pixel position:500.001; Color:rgba(222,221,217,1)
 

htmlHeader = """<!DOCTYPE html>
<html>
  <head>
    <title>A form talking to a python script</title>
    <style type="text/css">
    </style>
  </head>
  <body>"""

print "Content-Type: text/html"
print 
print htmlHeader

pat = r"(Pixel position:(\d\d\d).(\d\d\d)\;\ Color:(rgba\(.*\)))"

if not re.search(pat, text):			# if pattern 'pat' isn't found within text
    print "nothing found"
else:
    f = open("data/data.txt", 'a')  		# opens text file in apend mode
    for m in re.finditer(pat, text):		# for each match in text
        string = m.group(0)                     # m.group() lets you access data within the match object
	xPos = m.group(1)
	yPos = m.group(2)
	color = m.group(3)
	f.write(xPos + '\n')			# writes xPos to the text file plus newline
        print "thanks, url submitted. The pixel you are hosting has been added."
    f.close()
print """
   </body>
</html>"""