2017-10-29 364 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) 
相关问题