Note that there are some explanatory texts on larger screens.

plurals
  1. POMechanize + Python: how to follow a link in a simple javascript?
    primarykey
    data
    text
    <p>short: How to execute/simulate javascript redirection with python Mechanize?</p> <pre><code>location.href="http://www.site2.com/"; </code></pre> <p>I've made a python script with mechanize module that looks for a link in a page and follows it.</p> <p>The problem is on a particular site that when I do</p> <pre><code>br.follow_link("http://www.address1.com") </code></pre> <p>he redirects me to this simple page:</p> <pre><code>&lt;script language="JavaScript"&gt;{ location.href="http://www.site2.com/"; self.focus(); }&lt;/script&gt; </code></pre> <p>Now, if I do:</p> <pre><code>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 </code></pre> <p>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:</p> <pre><code>br.open("http://www.site2.com") </code></pre> <p>Site2 doesn't recognizes that I'm coming from "www.address1.com" and the script does not work as I would like!</p> <p>Sorry if it's just a newbie question and thank you in advance!</p> <p>p.s. I have br.set_handle_referer(True)</p> <p>EDIT: more info: Inspecting that link with Fiddler2 it looks like: </p> <blockquote> <p>GET <a href="http://www.site2.com/" rel="nofollow">http://www.site2.com/</a> HTTP/1.1 Host: www.site2.com Connection: keep-alive User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,<em>/</em>;q=0.8 Referer: <a href="http://www.address1.com" rel="nofollow">http://www.address1.com</a> Accept-Encoding: gzip,deflate,sdch Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4<br> Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: PHPSESSID=6e161axxxxxxxxxxx; user=myusername;<br> pass=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; ip=79.xx.xx.xx;<br> agent=a220243a8b8f83de64c6204a5ef7b6eb; __utma=154746788.943755841.1348303404.1350232016.1350241320.43; __utmb=154746788.12.10.1350241320; __utmc=154999999; __utmz=154746788.134999998.99.6.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=%something%something%</p> </blockquote> <p>so it seems to be a cookie problem?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload