爬虫:获取多次跳转后的页面url

  案例:页面中的一个链接,审核元素得到的地址是“http://iphone.myzaker.com/l.php?l=54472e161bc8e0fd4a8b4573” ,点击之后页面自动跳转到另一个地址“

http://mp.weixin.qq.com/s?__biz=MjM5NjExNjI4MA==&mid=202695292&idx=1&sn=8638f15ba27381236641077a77d43e03&scene=4#wechat_redirect”。

wget 分析地址 

apples-air:mzread apple$ wget http://iphone.myzaker.com/l.php?l=54472e161bc8e0fd4a8b4573
--2014-10-23 17:27:17--  http://iphone.myzaker.com/l.php?l=54472e161bc8e0fd4a8b4573
Resolving iphone.myzaker.com... 106.186.30.108
Connecting to iphone.myzaker.com|106.186.30.108|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://mp.weixin.qq.com/s?__biz=MjM5NjExNjI4MA==&mid=202695292&idx=8&sn=f39c6c5dc2329e41eb58c71b53ba8a50&scene=4#wechat_redirect [following]
--2014-10-23 17:27:19--  http://mp.weixin.qq.com/s?__biz=MjM5NjExNjI4MA==&mid=202695292&idx=8&sn=f39c6c5dc2329e41eb58c71b53ba8a50&scene=4
Resolving mp.weixin.qq.com... 203.205.143.142
Connecting to mp.weixin.qq.com|203.205.143.142|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 42622 (42K) [text/html]

  可以看到访问原地址之后,有一个302的跳转。

那么问题来了,怎么样获取到跳转之后的页面地址?

 

  办法:利用方法Net::HTTP.get_response。

  具体代码:

  

 require 'net/http'

 res=Net::HTTP.get_response(URI('http://iphone.myzaker.com/l.php?l=54472e161bc8e0fd4a8b4573'))

 res['location']
=> "http://mp.weixin.qq.com/s__biz=MjM5NjExNjI4MA==&mid=202695292&idx=1&sn=8638f15ba27381236641077a77d43e03&scene=4#wechat_redirect" 

  这样就可以得到跳转之后页面的url。  

转载于:https://www.cnblogs.com/wangyuyu/p/4046425.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值