求助:抓取两次跳转后的网页地址

delphy_de 2012-11-13 03:22:19
import sys,re,urllib2,cookielib
def download(url):
f = urllib2.urlopen(url)
redirectUrl=f.geturl()
print redirectUrl
rf=urllib2.urlopen(redirectUrl)
s=rf.read()
return s



s = download("http://www.fandongxi.com/shuo/r/3583515")
print s

上面是我的代码,我想获取http://www.fandongxi.com/shuo/r/3583515跳转后的最终网址,代码运行后会得到一次跳转后的redirectUrl:
http://s.click.taobao.com/t_js?tu=http%3A%2F%2Fs.click.taobao.com%2Ft%3Fe%3DzGU34CA7K%252BPkqB07S4%252FK0CITy7klxn%252F7bvn0ay1FVxZc%252FGkwbsiae%252Fa0JM%252BiP03z3ou84RRf29lEdMNQZlLT4ZNcBcHkFHsxIcfbeyvaZRm5n2tsWtm2BSOcMbmK47INk%252Bslct5wSYH0UffyPps7sbw7bCERj1B6W%252BAKa16YS699PDBDbokMPZX2yTTdMqDB%26spm%3D2014.12145346.1.0%26ref%3D%26et%3DjFBB3CpqmF6ZRQ%253D%253D
和以下网页源码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<script type='text/javascript'>
function htmlspecialchars(str){
str = str.replace(/</g, '<');
str = str.replace(/>/g, '>');
str = str.replace(/"/g, '"');
str = str.replace(/'/g, ''');
return str;
}
function bol(){
var inf = (top.location!=self.location);
var qs = location.search.split("?")[location.search.split("?").length-1].split("&");
qso = {};
for(var i=0;i<qs.length;i++){
if(qs[i]=="")continue;
var tmpa = qs[i].split("=");
qso[tmpa[0]] = tmpa[1]?tmpa[1]:"";
}
var tu = unescape(qso.tu);
if(htmlspecialchars(tu).length != tu.length){
exit;
}
if(inf){
var oi = document.createElement("iframe");
oi.id="iobj"
oi.border=0;
oi.frameBorder=0;
oi.style.height="1px"
oi.style.width="1px"
document.body.appendChild(oi);
var cd = oi.contentWindow.document;
cd.write('<ht'+'ml><bo'+'dy style="margin:0px;padding:0px"></scr'+'ipt><scr'+'ipt src="http://js.tongji.linezing.com/1023331/tongji.js" type="text/javascript"></scr'+'ipt><noscr'+'ipt><a href="http://www.linezing.com"><img src="http://img.tongji.linezing.com/1023331/tongji.gif"/></a></noscr'+'ipt></bo'+'dy></ht'+'ml>');
window.setTimeout(function(){cd.close();if(qso.tu)location.href = unescape(qso.tu);},1000);
}else{
if(qso.co&&qso.co!=""){
document.cookie="tk_trace="+(qso.co)+";path=/;domain=.taobao.com";
}
if(qso.tu && (qso.tu.indexOf("http%3A%2F%2Fs.click.taobao.com%2F")===0 || qso.tu.indexOf("http%3A%2F%2Fs.click.alimama.com%2F")===0
|| qso.tu.indexOf("http%3A%2F%2Fitem8.taobao.com%2F")===0 || qso.tu.indexOf("http%3A%2F%2Fshop8.taobao.com%2F")===0)){
if(!window.attachEvent){
document.write('<input style="display:none" type="button" id="exe" value="" onclick="window.location=\''+unescape(qso.tu)+'\'">');
document.getElementById('exe').click();
}else{
document.write('<a style="display:none" href="'+unescape(qso.tu)+'" id="exe"></a>');
document.getElementById('exe').click();
}
}
}
}//end of bol()
bol();
</script>
</body>
</html>

但这个源码不是我最终要得到的网页的源码
把redirectUrl放到浏览器里会得到:
http://item.taobao.com/item.htm?id=15933790628&ali_trackid=2:mm_22617315_0_0:1352791165_3k3_1596125318&spm=2014.12145346.1.0
这才算最终的网址,请求高人相助,这里应该是有javascript所有跳不过去,JS代码我看不太懂,网上有人说做个JS解密就可以跳过去,也尝试过Htmlunit模拟浏览器,也没有解决,可能是我没有用对,总之有办法得到最后的网址就行
希望有人能帮到我,谢谢~···
...全文
485 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gloveing 2012-11-19
  • 打赏
  • 举报
回复
方法1:直接看JS代码,了解跳转的时候是跳转到哪里 方法2:使用COM操作IE,这样就不必管他怎么跳转了,最后获取源码就行了 方法3:使用Fiddle查看跳转的HTTP请求
crifan 2012-11-15
  • 打赏
  • 举报
回复
已经在: 怎么得到这个网址跳转后的源码 中回复过你了,自己去看吧。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧