python - CookieConflictError - 尝试以编程方式登录 Google 财经时出现 APISID

标签 python python-requests google-finance

我正在尝试编写一个脚本,以编程方式登录 Google 财经、查看我的投资组合,然后在桌面上显示结果。我正在使用 requests 模块,目前卡在“登录”部分。

我不断收到此错误requests.cookies.CookieConflictError:有多个名称为“APISID”的cookie

这是整个脚本,错误在第 48 行上抛出。我猜这与requests keep-alive有关连接未正确回收?

#!/usr/bin/env python

import getpass
import re
import requests

email = raw_input("Enter your Google username: ")
password = getpass.getpass("Enter your password: ")

session = requests.Session()

# Define URLs
login_page_url = 'https://accounts.google.com/ServiceLogin?passive=true&service=finance'
authenticate_url = 'https://accounts.google.com/ServiceLoginAuth?service=finance'
gf_home_page_url = 'http://www.google.com/finance/portfolio'

login_page_contents = session.get(login_page_url).text

# Find GALX value
galx_match_obj = re.search(r'name="GALX"\s*value="([^"]+)"', login_page_contents, re.IGNORECASE)
galx_value = galx_match_obj.group(1) if galx_match_obj.group(1) is not None else ''

# Find DSH value
dsh_match_obj = re.search(r'id="dsh"\s*value="([^"]+)"', login_page_contents, re.IGNORECASE)
dsh_value = dsh_match_obj.group(1) if dsh_match_obj.group(1) is not None else ''

# Set up login credentials
login_params = {
   'Email': email,
   'Passwd': password,
   'continue': 'http://www.google.com/finance/portfolio',
   'followup': 'http://www.google.com/finance/portfolio',
   'service': 'finance',
   'GALX': galx_value,
   'pstMsg': 0,
   'dnConn': '',
   'checkConnection': '',
   'timeStmp': '',
   'secTok': '',
   'bgresponse': 'js_disabled',
   'PersistentCookie': 'yes'
}

print galx_value
print dsh_value

# Login
r = session.post(authenticate_url, params=login_params) # <- Error thrown here
print r.text
exit

回溯:

Traceback (most recent call last):
  File "crawl.py", line 48, in <module>
    r = session.post(authenticate_url, params=login_params)
  File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/sessions.py", line 358, in post
    return self.request('POST', url, data=data, **kwargs)
  File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/sessions.py", line 312, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/sessions.py", line 426, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/sessions.py", line 163, in resolve_redirects
    resp.cookies.update(cookiejar)
  File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_abcoll.py", line 494, in update
    self[key] = other[key]
  File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/cookies.py", line 246, in __getitem__
    return self._find_no_duplicates(name)
  File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/cookies.py", line 285, in _find_no_duplicates
    raise CookieConflictError('There are multiple cookies with name, %r' % (name))
requests.cookies.CookieConflictError: There are multiple cookies with name, 'APISID'

最佳答案

这是请求中的一个错误,请参阅issue 1189 .

当前建议的修复是简单地删除 requests/sessions.py 的第 163 行:

resp.cookies.update(cookiejar)

关于python - CookieConflictError - 尝试以编程方式登录 Google 财经时出现 APISID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14873277/

相关文章:

python - 操作系统级的改变可以加速 Python 的多重处理?

python - opencv - 如何在没有比例的情况下进行模板匹配?

Python 请求模块发送 JSON 字符串而不是 x-www-form-urlencoded 参数字符串

python 请求,我得到的响应状态与 postman 的结果不同。

google-finance - 如何使用 Google 财经?

google-sheets - 如何从谷歌财经获取特定日期的股票价格?

python - 在 Pydub 和 Librosa 之间转换音频文件

python - Python 集合消耗的内存量逐步增加

networking - 一台机器可以从多个不同的IP地址发出请求吗?

python - 使用 bs4 搜索特定类