2017-10-29 157 views
-1

我想使用python代碼自動獲取API密鑰。 這是我手工獲取api密鑰的方法。如何設置請求cookie何時使用python3發佈表單?

  1. 通過手:

    1. 打開https://www.alphavantage.co在Firefox
    2. 點擊Get your Free API Key Today
    3. 輸入first_namelast_nameemail
    4. 點擊get free api key

enter image description here

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密鑰?

target website

+0

你好,從我的理解,一旦你得到的此網站的API密鑰,您可以繼續使用該API密鑰。然後,您使用該API密鑰來查詢服務器。 –

回答

1
import urllib.request, urllib.parse, urllib.error 
import http.cookiejar 

LOGIN_URL1 = 'https://www.alphavantage.co/support/#api-key' 

cookie = http.cookiejar.MozillaCookieJar() 
handler = urllib.request.HTTPCookieProcessor(cookie) 
opener = urllib.request.build_opener(handler) 
request = urllib.request.Request(LOGIN_URL1) 
response = opener.open(request) 
for item in cookie: 
    x=item.value 

LOGIN_URL2 = 'https://www.alphavantage.co/create_post/' 
params = { 
"first_text": "xx", 
"last_text": "yy", 
"occupation_text": "Investor", 
"email_text": "[email protected]" 
} 

headers = { 
"Referer": "https://www.alphavantage.co/support/", 
"Cookie": "csrftoken={0}".format(x), 
"X-CSRFToken":"{0}".format(x) 
} 

postdata = urllib.parse.urlencode(params).encode() 
req = urllib.request.Request(LOGIN_URL2, postdata, headers) 
response = urllib.request.urlopen(req) 
print(response.read()) 
0

有在你的代碼兩個錯誤。

首先。 POST請求應發送到/create_post/端點。

二。字段名稱是不同的:

params = { 
    "first_text": "xx", 
    "last_text": "yy", 
    "occupation_text": "investor", 
    "email_text":"[email protected]" 
} 

而你只需要三個頭:RefererCookieX-CSRFToken

全碼:

import requests 


headers = { 
    "Referer": "https://www.alphavantage.co/support/", 
    "Cookie": "csrftoken=YOURTOKEN", 
    "X-CSRFToken":"YOURTOKEN" 
} 
params = { 
    "first_text": "a", 
    "last_text": "b", 
    "occupation_text": "Student", 
    "email_text": "[email protected]" 
} 
response = requests.post("https://www.alphavantage.co/create_post/", 
         data=params, headers=headers) 
print(response.text) 
相關問題