1

short: How to execute/simulate javascript redirection

location.href="http://www.site2.com/";

with python Mechanize?

I've made a python script with mechanize module that looks for a link in a page and follows it.

The problem is on a particular site that when I do

br.follow_link("http://www.address1.com") 

he redirects me to this simple page:

<script language="JavaScript">{                                                                                         
    location.href="http://www.site2.com/";                                                                                           
    self.focus();                                                                                                                   
    }</script>

Now, if I do:

br = mechanize.Browser(factory=mechanize.RobustFactory())

... #other code

br.follow_link("http://www.address1.com") 
for link in br.links():   
br.follow_link(link)
print link

it doesn't prints anything, that means that there is no link in that page.

But if I manually parse the page and I execute:

br.open("http://www.site2.com")

Site2 doesn't recognizes that I'm coming from "www.address1.com" and the script does not work as I would like!

Sorry if it's just a newbie question and thank you in advance!

3
  • that means that there isn't such link in that page. No it doesn't, it means that link is a string object. Commented Oct 7, 2012 at 16:28
  • Yeah, sorry, corrected, my head is melted. Commented Oct 7, 2012 at 16:52
  • Solved here: stackoverflow.com/questions/12881423/… Commented Oct 15, 2012 at 19:53

1 Answer 1

0

Try adding

br.set_handle_referer(True)

after the constructor call.

I recommend you these options :

br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.