推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
nan0kai
V2EX  ›  Python

请教一个 Python requests 获取淘宝链接跳转后的真实地址的实现方法

  •  
  •   nan0kai · Jun 7, 2017 · 4110 views
    This topic created in 3441 days ago, the information mentioned may be changed or developed.

    链接地址如

    https://s.click.taobao.com/WRoeUkw 
    

    跳转成功的 URL 是

    https://item.taobao.com/item.htm?id=548674077854&ali_trackid=2:mm_16389311_20848374_70766594:1496849954_2k8_1774242087&pvid=19_183.204.96.103_711_1495772079051
    

    请问,如果用 requests 或者是 pyspider 得到后面的地址,最好有分析思路.

    学习爬虫小白,感谢指导。

    Supplement 1  ·  Jun 8, 2017
    感谢三位的热心回复,按二楼 @wanglaihuai 提供的思路方法 只能得到第一次跳转后的链接,截图如下

    http://i1.piimg.com/4297/befaefcde74e0dae.jpg 怎样获取第二次跳转的 url

    同样很感谢 @linkiosk 的回复。
    Supplement 2  ·  Jun 8, 2017

    上图似乎挂了,补充图片

    第二次requests.get(url).url 获取不到想要的链接地址

    3 replies  •  2017-06-08 07:09:01 +08:00
    misaka19000
    misaka19000
    Jun 7, 2017❤️1
    1


    做了几次重定向而已,检测到状态码是 302 就把 location 参数提取出来作为新的请求链接
    wanglaihuai
    wanglaihuai
    Jun 8, 2017 via Android❤️1
    2
    requests.get(url).url 就是跳转的地址了。
    linkiosk
    linkiosk
    Jun 8, 2017 via iPhone❤️1
    3
    二楼正解
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   546 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:05 · PVG 06:05 · LAX 14:05 · JFK 17:05
    ♥ Do have faith in what you're doing.