0

For the past couple of days I have been thinking about a way to make a website that finds if a show is currently running using BeautifulSoup to scrape wikipedia. I currently have the BeautifulSoup scraping correctly done but I cannot make the html run the python script and then update the html with the result. All I can do is make another page on a website using cgi that displays it (which I don't want).

All i'm wondering is how I can make my code do this: display html -> run python script -> return python script result -> update html using ajax

Heres what I have so far:

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">   </script>
</head>

<body>
    <form action="/cgi-bin/hello_get.py" id="#my_text" name="test" method="get">
    <input type = "text" id="my_text" name="val">
    <input type="button" id="my_button" value="click me">
    </form>

<div id="result">

</div>

<script>
$("#my_button").click(function(){
 $("#result").load("/cgi-bin/hello_get.py?val=my_text"+$("#my_text").val())
})
</script>

</body>
</html>

and the python code:

#!/usr/bin/python

# Import modules for CGI handling 
import cgi, cgitb,requests, re
from bs4 import BeautifulSoup

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
formInput = form.getvalue('val')

def test(form):
    r = requests.get(form)

    soup = BeautifulSoup(r.content)

    date = soup.find_all("table", {"class": "infobox"})

    for item in date:
        dates = item.find_all("th")
        for item2 in dates:
            if item2.text == "Original run":
                test2 = item2.find_next("td").text.encode("utf-8")
                mysub = re.sub(r'\([^)]*\)', '', test2)
                return mysub

test(formInput)

2 Answers 2

1

You can build a simple web application with the Python Flask framework. You can:

  • Run your script
  • Save the output in a variable
  • Write the variable contents to a HTTP response

You can start with the following: MyFunction.py, where you'll write your existing code.

In the same directory create a server.py

from flask import Flask, jsonify, render_template, request, Response
from MyFunction import test
app = Flask(__name__)

@app.route("/")
#Sample code to call test   
def some_function():
    return Response(test(someInput)) #writes to Http response

You can now call this code using ajax and pass values to the server and the response will be used to update html.

If you're planning to use it for the web, you can then probably host this Flask code on Heroku or something similar. Here is method that's creating RESTful web API using Flask. You can read through it and understand the workings of it. http://blog.luisrei.com/articles/flaskrest.html

Sign up to request clarification or add additional context in comments.

Comments

0

You will need a small web framework to do this. I would choose flask. Here is an example of a small Flask setup that can recieve, process and reply to ajax requests.

http://flask.pocoo.org/docs/0.10/patterns/jquery/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.