代码之家  ›  专栏  ›  技术社区  ›  showkey

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

  •  -1
  • showkey  · 技术社区  · 7 年前

    我想用python代码自动获取api密钥。 下面是我手动获取api密钥的步骤。

    1. 手动:

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

    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
    

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

    target website

    2 回复  |  直到 7 年前
        1
  •  4
  •   user7988893 user7988893    7 年前
    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())
    
        2
  •  0
  •   Alexey    7 年前

    您的代码中有两个错误。

    第一POST请求应发送至 /create_post/

    第二字段名称不同:

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

    您只需要三个标题: Referer , Cookie , X-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)