[Tutor] website is returning error when I post data

Benjamin Fishbein bfishbein79 at gmail.com
Sat Oct 6 21:00:07 CEST 2012


Hello. This problem has got me stumped, and I've been trying to figure it out for more than a month.
This program checks to see if a college buyback site is buying back particular books.
I'm using mac OSX
Here's the code.

import urllib,urllib2
base_url="http://textbooks.com"
action="/BuyBack-Search.php?CSID=AQ2ZJKUWKZJBSD2T2DDMKMKB"
url=base_url+action
name="bb_isbns"
isbns="""0891917608 0307387895 0195070658 0137806922 074324754X\
 067149399x 0446556246 031604993X 0070322538"""
data={name:isbns}
encoded_data=urllib.urlencode(data)
text=urllib2.urlopen(url,encoded_data).read()
file_name="textbooks_trial.txt"
file=open(file_name,"w")
file.write(text)
file.close()
print "finished running program"
print "check file 'textbooks_trial.txt'"

When I go to the website(without using Python) and I paste the text (isbns) into the text box then enter it, it comes back with the error:
  Oops... there may be a mistake here.  
Sorry, your search could not be completed at this time. Please try again. (Error: BB-09-00-05)

But the text I entered is still in the text entry box. When I click on the orange button ("Sell your Textbooks") a second time, it goes through.

The problem is that when I try to do this through Python, it returns the code with the error, and there's no orange button to click.
I tried submitting it again, hoping the second time would work, using the following code:

import urllib,urllib2
text="there may be a mistake here"
base_url="http://textbooks.com"
action="/BuyBack-Search.php?CSID=AQ2ZJKUWKZJBSD2T2DDMKMKB"
url=base_url+action
name="bb_isbns"
isbns="""0891917608 0307387895 0195070658 0137806922 074324754X\
 067149399x 0446556246 031604993X 0070322538"""
data={name:isbns}
encoded_data=urllib.urlencode(data)
while "there may be a mistake here" in text:
    text=urllib2.urlopen(url,encoded_data).read()
    file_name="textbooks_trial.txt"
    file=open(file_name,"w")
    file.write(text)
    file.close()
    print "tried doing the program"
print "finished running program"
print "check file 'textbooks_trial.txt'"

The result is an endless loop, where it prints out:
"tried running the program"

If anyone can figure out what I need to do, I'd be extremely grateful.
Thanks,
Ben


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121006/da91851b/attachment.html>


More information about the Tutor mailing list