最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python中用requests获取到一个url地址的302跳转后的真正有效地址

地址 crifan 1103浏览 0评论
折腾:
【未解决】自动化搜索和提取安卓游戏网站game.vivo.com.cn中游戏信息列表
期间,需要去获取
http://dl.gamecenter.vivo.com.cn/clientRequest/gameDownload?id=55002&pkgName=com.tatuyin.rxsg.vivo&sourword=%E4%B8%89%E5%9B%BD&page_index=2&dlpos=1&channel=h5
这种 临时的下载地址,经过302跳转后的真正的地址:
去Chrome中分析就是:
此处去试试代码实现
python requests 302
Requests and the HTTP 302 Status Code • Lukasa’s Echochamber
设置allow_redirects为false?即可?
redirect – python requests handle error 302? – Stack Overflow
去看看resp.history
http – Python Requests library redirect new url – Stack Overflow
感觉还是:不让跳转 更好?
r = requests.get('http://github.com/', allow_redirects=False)
去试试
def get302RealUrl(originUrl):
    realUrl = ""
    resp = requests.get(originUrl, allow_redirects=False)
    if resp.status_code == 302:
        realUrl = resp.headers['Location']
    return realUrl
即可:
然后提取到库中:
import requests

def get302RealUrl(originUrl):
    """get real url address after 302 move


    Args:
        originUrl (str): original url
    Returns:
        real url(str)
    Raises:
    Examples:
        input: 'http://dl.gamecenter.vivo.com.cn/clientRequest/gameDownload?id=57587&pkgName=com.jiuzun.mxsg.vivo&sourword=%E4%B8%89%E5%9B%BD&page_index=4&dlpos=1&channel=h5'
        output: 'https://gameapktxdl.vivo.com.cn/appstore/developer/soft/20180206/201802061851104837232.apk'
    """
    realUrl = ""
    resp = requests.get(originUrl, allow_redirects=False)
    if resp.status_code == 302:
        realUrl = resp.headers['Location']
    return realUrl
详见:
https://github.com/crifan/crifanLibPython/blob/master/crifanLib/crifanHttp.py

转载请注明:在路上 » 【已解决】Python中用requests获取到一个url地址的302跳转后的真正有效地址

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.192 seconds, using 23.38MB memory