Mechanize+Python:如何在简单的javascript中跟踪链接?

2024-06-17 13:35:55 发布

您现在位置:Python中文网/ 问答频道 /正文

short:如何使用python Mechanize执行/模拟javascript重定向?

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

我制作了一个带有mechanize模块的python脚本,该模块在页面中查找链接并跟踪它。

问题是在一个特定的网站上

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

他把我重定向到这个简单的页面:

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

现在,如果我这样做了:

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

它不会打印任何内容,这意味着该页中没有链接。 但如果我手动解析页面并执行:

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

Site2无法识别我来自“www.address1.com”,并且脚本无法按我的意愿工作!

抱歉,如果这只是一个新手的问题,并提前谢谢你!

p.s.I have br.set_handle_referer(真)

编辑:更多信息: 用Fiddler2检查这个链接看起来像:

GET http://www.site2.com/ 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,/;q=0.8 Referer: http://www.address1.com Accept-Encoding: gzip,deflate,sdch Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: PHPSESSID=6e161axxxxxxxxxxx; user=myusername;
pass=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; ip=79.xx.xx.xx;
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%

所以看起来是饼干的问题?


Tags: brcomhttp链接wwwlinklocation页面