【问题标题】:Python Retrieve cookie sent in a Request HeaderPython 检索请求标头中发送的 cookie
【发布时间】:2021-03-11 01:58:09
【问题描述】:

您好,我想自动化一个从网站下载文件的过程,该网站有一个表单登录 当我使用浏览器时,我可以在 Request Http Header 中看到一个 cookie。这似乎是成功授权所必需的。否则我最终会出现 401 错误。 即使我两次发送请求,它也不起作用,因为第一个响应不包含所需的 cookie。 如果可以使用 python 从 Request Http Header 获取 cookie 的任何建议。

登录网址: https://services.geoplace.co.uk/login

下载所需文件的网址: https://services.geoplace.co.uk/api/downloadMatrix/getFile?fileName=30001_81s3.zip&fileType=LEVEL_3&fileVersion=May-2020&sfAccountId=xxx

import mechanize
import cookielib
from bs4 import BeautifulSoup as bs
import html2text
import html5lib#
import sys

# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

br.addheaders = [('User-agent', 'Chrome')]

# The site we will navigate into, handling it's session
br.open('https://login.geoplace.co.uk/login')

# View available forms
for f in br.forms():
    print "Formm " + str(f)

# Select the second (index one) form (the first form is a search query box)
br.select_form(nr=0)

# User credentials
br.form['username'] = 'myusername'
br.form['password'] = 'mypassword'

# Login
response = br.submit()

br.open('https://services.geoplace.co.uk')
request = br.request
print request.header_items()

# if successful we have some cookies now
cookies = br._ua_handlers['_cookies'].cookiejar
# convert cookies into a dict usable by requests
cookie_dict = {}
for c in cookies:
    cookie_dict[c.name] = c.value
print cookie_dict


br.open('https://services.geoplace.co.uk/api/downloadMatrix/getFile? 
fileName=30001_81s3.zip&fileType=LEVEL_3&fileVersion=May- 
2020&sfAccountId=xxx')

【问题讨论】:

  • 如果您提供您当前的代码或信息(例如您正在使用的 http 客户端)会更好。我建议你查看 python 'requests' 库。
  • stackoverflow.com/questions/7164679/… 这可能会有所帮助。
  • 我已经添加了我正在使用的代码。登录后,我期望获得下一个请求所需的所有 cookie。但看起来并非如此。
  • 如果设置了 cookie 或它是否为空,您是否检查了“cj”?
  • 是的,实际上cookies已经设置好了(见底部的代码)。然而,它们似乎并不正确。这就是为什么最后一行因 401 错误而失败的原因。我应该期望 cookie 类似于浏览器吗?

标签: python cookies request header


【解决方案1】:

您上面提到的 api 支持 OAuth2 ( Client , Password ) 授权类型。如果您向 GeoPlace 寻求帮助(通过此电子邮件 support@geoplace.co.uk) - 我们将根据您的请求创建客户端凭据,您应该能够访问它(我们有其他实体以这种方式使用我们的服务)

获得凭据后,步骤如下

  1. curl 'https://login.geoplace.co.uk/oauth/token' -H "Authorization: Basic ZZZZZZZZZZZZZZZZZZZ" -d username='xxxxxxx' -d password='yyyyyyy' -d grant_type=password

    (这将返回您的令牌信息)

  2. 使用上面的令牌执行curl -H "Authorization: Bearer 66666-yyy-Ysdf-bb-xxxxx" -o 'FILE_NAME_TO_SAVE_IN_LOCAL.zip' 'https://services.geoplace.co.uk/api/downloadMatrix/getFile?fileName=30001_81s3.zip&fileType=LEVEL_3&fileVersion=May-2020&sfAccountId=xxx'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2016-06-06
    相关资源
    最近更新 更多