我想使用python代碼自動獲取API密鑰。 這是我手工獲取api密鑰的方法。如何設置請求cookie何時使用python3發佈表單?
通過手:
- 打開
https://www.alphavantage.co
在Firefox - 點擊
Get your Free API Key Today
- 輸入
first_name
last_name
email
- 點擊
get free api key
。
- 打開
2.By代碼。
import urllib.request, urllib.parse, urllib.error
import http.cookiejar
LOGIN_URL = 'https://www.alphavantage.co/support/#api-key'
params = {
"first": "xx",
"last": "yy",
"occupation": "investor",
"email":"[email protected]"
}
headers = {
"Accept-Language":"en-US,en;q=0.8",
"Connection":"keep-alive",
"Content-Length":"77",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"Cookie":"csrftoken=qTbVt3HN2VYiDbJgX1n9DdyaDUYKpMyJ1UvTE3xCplYZcAYk9OQaXJ1F6ACadcjA; _ga=GA1.2.1054357644.1509295038; _gid=GA1.2.1986003924.1509295038; _gat=1",
"Host":"www.alphavantage.co",
"Origin:https":"//www.alphavantage.co",
"Referer:https":"//www.alphavantage.co/support/",
"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36",
"X-CSRFToken":"l7RRVpYomq6fIvjAnuYJiR0xquqoeD5gXrlowpQqejCCKX65OUrUcZzw2ljf9SPB",
"X-Requested-With":"XMLHttpRequest"
}
postdata = urllib.parse.urlencode(params).encode()
user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
cookie = http.cookiejar.MozillaCookieJar()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
request = urllib.request.Request(LOGIN_URL, postdata, headers)
response = opener.open(request)
錯誤信息:
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 499: Client Disconnected
該Cookie在頭部是一個請求的cookie,在step1.2創建。
如何用python代碼而不是手動獲取api密鑰?
你好,從我的理解,一旦你得到的此網站的API密鑰,您可以繼續使用該API密鑰。然後,您使用該API密鑰來查詢服務器。 –