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)