You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python解码含同意信息的Google News Base64 URL遇报错问题

解码含同意信息的Google News Base64 URL问题

我用Python解码Google News中包含同意信息的Base64 URL时遇到了特殊问题。之前参考StackOverflow旧问题《decoding Google news urls》编写的函数可以正常解码99%的URL,但遇到包含同意信息的编码URL时会触发异常。

举个例子,有一段276字符的Base64串:
CBMiYWh0dHBzOi8vd3d3LnRpbWVzb2Zpc3JhZWwuY29tL2Zvci15ZWFycy1uZXRhbnlhaHUtcHJvcHBlZC11cC1oYW1hcy1ub3ctaXRzLWJsb3duLXVwLWluLW91ci1mYWNlcy_SAWVodHRwczovL3d3dy50aW1lc29maXNyYWVsLmNvbS9mb3IteWVhcnMtbmV0YW55YWh1LXByb3BwZWQtdXAtaGFtYXMtbm93LWl0cy1ibG93bi11cC1pbi1vdXItZmFjZXMvYW1wLw==

脚本解码时提示长度不是4的倍数,无法完成解码,但在线解码器可以解析出同意声明及目标URL。我现在无法在脚本中获取解码结果,也就没法剥离其中的同意信息。

附上相关函数代码:

def decode_google_url(e):
    global faulty_urls
    faulty_urls=[]
    # trim leading/trailing whitespace
    e = e.strip()
    # decode string to get target URL
    try:
        target_url = base64.b64decode(e)[4:].decode('utf-8', "backslashreplace").split('\\')[0]
        target_urls.append(target_url)
    except Exception as ex:
        print(f"Error decoding URL: {ex}")
        # all exceptions are triggered by links that contain consent information as well as URLs
        faulty_urls.append(e)

    return faulty_urls
    return target_urls

内容的提问来源于stack exchange,提问作者OnceUponATime

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 21:42:21