Python 如何将数据发布到网站,存储cookies,然后获取另一个页面

Python 如何将数据发布到网站,存储cookies,然后获取另一个页面,python,cookies,flask,mechanize,Python,Cookies,Flask,Mechanize,我试着用Mechanize解决这个问题,但没能让它工作 只有在登录后发送cookie时,网站才允许访问数据。我需要做以下工作: 使用“发布到页面”登录 储存饼干 访问保护页 我使用Mechanize提出了以下解决方案。Cookie由mechanize.Browser管理 br = mechanize.Browser() resp = br.open('https://xxxxxxxxxxxxxxxxxxx') br.select_form(nr=0) br['username'] = us

我试着用Mechanize解决这个问题,但没能让它工作

只有在登录后发送cookie时,网站才允许访问数据。我需要做以下工作:

  • 使用“发布到页面”登录
  • 储存饼干
  • 访问保护页

  • 我使用Mechanize提出了以下解决方案。Cookie由
    mechanize.Browser
    管理

    br = mechanize.Browser()   
    resp = br.open('https://xxxxxxxxxxxxxxxxxxx')
    br.select_form(nr=0)
    br['username'] = username
    br['password'] = password
    response = br.submit()
    time.sleep(1)
    resp_second = br.open('https://secretwebpage')
    print resp_second.read()
    
    您可以在中使用会话。从:

    Session对象允许您跨多个会话持久化某些参数 请求。它还将cookie保存在从服务器发出的所有请求中 会话实例

    以下是登录和后续请求的外观:


    请具体说明。什么是网站?您尝试过什么?您不应该只是要求我们为您编写代码,而应该向我们展示您已经尝试过的操作,然后我们可以帮助您解决它的问题。
    import requests
    
    s = requests.Session(verify='my_cert_file.crt')
    r = s.post('https://secure-site.com/login', data={
        'username': my_username,
        'password': my_password,
    })
    # logging in sets a cookie which the session remembers
    print s.cookies
    r = s.get('https://secure-site.com/secure-data')
    print r.json()