如何在 post 形式 python3 时设置请求 cookie?

How to set the request cookie when to post form with python3?

我想使用 python 代码自动获取 api 密钥。 这是我手动获取 api 密钥的方法。

  1. 手工:

    1. 在 firefox
    2. 中打开 https://www.alphavantage.co
    3. 点击Get your Free API Key Today
    4. 输入first_namelast_nameemail
    5. 单击 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":"zz@qq.com"
}

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

headers中的cookie是请求cookie,在step1.2中创建。

如何使用 python 代码而不是手动获取 api 密钥?

target website

你的代码有两个错误。

首先。 POST 请求应发送到 /create_post/ 端点。

其次。字段名称不同:

params = {
    "first_text": "xx",
    "last_text": "yy",
    "occupation_text": "investor",
    "email_text":"zz@qq.com"
}

而你只需要三个 headers: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": "aa@bb.cc"
}
response = requests.post("https://www.alphavantage.co/create_post/",
                         data=params, headers=headers)
print(response.text)
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": "cc@bb.cc"
}

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())