使用不和谐请求页面中的 url 并在标头中添加用户代理,我能够得到响应:
import urllib.request
url = 'https://discord.com/api/v6/invites/UWxaV8m?with_counts=true'
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':user_agent,}
request=urllib.request.Request(url,None,headers) #The assembled request
response = urllib.request.urlopen(request)
data = response.read() # The data u need
print(data)
输出:
b'{"code": "UWxaV8m", "guild": {"id": "599754844676554783", "name": "Thicc Mario Hub | 10 Boosts", "splash": "1d76b7a1ecceffa42552abc88ea5d6ad", "banner": "7036d8cd5e36e91fe5c87478c5eae37a", "description": null, "icon": "a_3355e47f1d336ad04737afc2ba08fe62", "features": ["ANIMATED_ICON", "INVITE_SPLASH"], "verification_level": 2, "vanity_url_code": null}, "channel": {"id": "599761618720653313", "name": "\\u2554\\u27a4\\u300e\\ud83d\\udcdc\\u300frules", "type": 0}, "inviter": {"id": "429840967470678037", "username": "Thicc Mario", "avatar": "dd33989a2b86a43f53280174addd3a21", "discriminator": "8611", "public_flags": 256}, "approximate_member_count": 1662, "approximate_presence_count": 373}'