Quantcast

Jump to content


Photo

Help with Python Mechanize and Shop Wiz


  • Please log in to reply
2 replies to this topic

#1 Poooppe

Poooppe
  • 2 posts

Posted 18 May 2015 - 06:34 PM

Hey guys,

I'm working on some shop wiz search functions using Mechanize but am running into some problems.

import re
import mechanize
import cookielib
import time
from mechanize import ParseResponse

br = mechanize.Browser()

br.set_handle_robots(False)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)

cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

br.set_proxies({"https": "207.188.135.38:38328"})
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0')]

br.open("http://www.neopets.com/market.phtml?type=wizard")


br.select_form(name="login")
# Browser passes through unknown attributes (including methods)
# to the selected HTMLForm.
br["username"] = REDACTED  # (the method here is __setitem__)
br["password"] = REDACTED


# Submit current form.  Browser calls .close() on the current response on
# navigation, so this closes response1
after_login = br.submit()

print br.title()
print after_login.geturl()

# print currently selected form (don't call .submit() on this, use br.submit())
f = open('after_login.html', 'w')
f.write(after_login.read())
f.close()

#for form in br.forms():
#	print form

br.select_form(nr=1)

#br["type"] = "process_wizard"
#br["feedset"] = "0"
br["shopwizard"] = "cheese"
br["table"] = ["shop",]
br["criteria"] = ["containing",]
br["min_price"] = "0"
br["max_price"] = "99999"

time.sleep(5);
after_search = br.submit()

print br.title()
print after_search.geturl()

#print after_search.read()
f = open('after_search.html', 'w')
f.write(after_search.read())
f.close()


Opening after_search.html provides the error Error: You have been directed to this page from the wrong place! If you KEEP getting this error, chances are you have some security settings enabled that are not letting you play Neopets correctly.

Click here to see some tips that might help you fix this problem."

Can someone please explain what I'm doing wrong and how to fix it? Thanks

 

Also, I look at the packets sent using mechanize and Firefox and they both apear to send the same form data.


Edited by Poooppe, 18 May 2015 - 06:40 PM.


#2 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 18 May 2015 - 08:37 PM

That error would mean that the referrer being sent isn't what it should be.

#3 Poooppe

Poooppe
  • 2 posts

Posted 19 May 2015 - 02:20 PM

Thank you!

It works if you manually set the referer by doing

br.set_handle_referer(False)

br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0'),
         ('Referer', 'http://www.neopets.com/market.phtml?type=wizard')]

Instead of

br.set_handle_referer(True)

br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0')]

New code for future readers

import re
import mechanize
import cookielib
import time
from mechanize import ParseResponse

br = mechanize.Browser()

br.set_handle_robots(False)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(False)

#cj = cookielib.LWPCookieJar()
#br.set_cookiejar(cj)

br.set_proxies({"https": "207.188.135.38:38328"})
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0'),
		 ('Referer', 'http://www.neopets.com/market.phtml?type=wizard')]

br.open("http://www.neopets.com/market.phtml?type=wizard")


br.select_form(name="login")
# Browser passes through unknown attributes (including methods)
# to the selected HTMLForm.
br["username"] = REDACTED  # (the method here is __setitem__)
br["password"] = REDACTED


# Submit current form.  Browser calls .close() on the current response on
# navigation, so this closes response1
after_login = br.submit()

print br.title()
print after_login.geturl()

# print currently selected form (don't call .submit() on this, use br.submit())
f = open('after_login.html', 'w')
f.write(after_login.read())
f.close()

#for form in br.forms():
#	print form

br.select_form(nr=1)

#br["type"] = "process_wizard"
#br["feedset"] = "0"
br["shopwizard"] = "cheese"
br["table"] = ["shop",]
br["criteria"] = ["containing",]
br["min_price"] = "0"
br["max_price"] = "99999"

#time.sleep(5);
after_search = br.submit()

print br.title()
print after_search.geturl()

#print after_search.read()
f = open('after_search.html', 'w')
f.write(after_search.read())
f.close()





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users