首页 > 编程问答 >解码包含同意信息的 Google 新闻网址时出现问题

解码包含同意信息的 Google 新闻网址时出现问题

时间:2024-07-25 13:37:32浏览次数:10  
标签:python base64 google-news

我在 Python 中解码 base64 谷歌新闻 URL 时遇到了一个特殊问题,因为它们不仅包含 URL,还包含同意信息。

基于较早的问题 解码 Google 新闻 url ,我在其中编写了以下函数一个更大的脚本,可以正确解码 99% 的 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

正如您在评论中所看到的,异常是由似乎也包含同意信息的编码 URL 触发的。一个例子是以下 276 个字符的字符串,我的脚本中的 base64 不会对其进行解码,因为据称它不代表 4 的倍数:

CBMiYWh0dHBzOi8vd3d3LnRpbWVzb2Zpc3JhZWwuY29tL2Zvci15ZWFycy1uZXRhbnlhaHUtcHJvcHBlZC11cC1oYW1hcy1ub3ctaXRzLWJsb3duLXVwLWluLW91ci1mYWNlcy_SAWVodHRwczovL3d3dy50aW1lc29maXNyYWVsLmNvbS9mb3IteWVhcnMtbmV0YW55YWh1LXByb3BwZWQtdXAtaGFtYXMtbm93LWl0cy1ibG93bi11cC1pbi1vdXItZmFjZXMvYW1wLw==

当我将其放入在线解码器时,我得到以下信息:

“我已年满 18 岁,并且我同意根据本网站的隐私政策处理我的个人数据。 https://www.timesofisrael.com/for-years-netanyahu-proppped-up-hamas -now-its-blown-up-in-our-faces-

标签:python,base64,google-news
From: 78354679

相关文章