如何设置请求cookie何时使用python3发布表单?

时间:2017-10-29 06:14:04

标签: python cookies request response

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

  1. 手工:

    1. 在firefox中打开https://www.alphavantage.co
    2. 点击Get your Free API Key Today
    3. 输入first_name last_name email
    4. 点击get free api key
  2. enter image description here

    2.通过代码。

    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
    

    标题中的cookie是一个请求cookie,在步骤1.2中创建。

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

    target website

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

您的代码中存在两个错误。

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

二。字段名称不同:

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

而且您只需要三个标题: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)