如何将饼干腌制到现有列表中?

时间:2015-12-18 17:02:53

标签: python cookies pickle

我正在尝试将一些饼干腌制到现有的腌制清单中,但我一直收到错误。

twitter_loggins = []
pickle_path = "/home/ro/A_Python_Scripts/twitter/twitter2/twitter_loggins.pkl"
driver = webdriver.Firefox()
driver.get("https://twitter.com")
cookies = []

email = raw_input("Email used?")
cookies1 = driver.get_cookies
cookie = [email, cookies1]
cookies.append(cookie)

if os.path.exists(pickle_path):
    with open(pickle_path, 'rb') as loggins:
        cookies1 = pickle.load(loggins)
        for cookie in cookies1:
            cookies.append(cookie)

with open(pickle_path, 'wb') as file1:
    pickle.dump(cookies, file1)

我的工作代码可以腌制Cookie。

driver = webdriver.Firefox()
driver.get("https://twitter.com")
continu = raw_input("Press Enter when you've logged in")
pickle.dump(driver.get_cookies() , open("/home/ro/A_Python_Scripts/twitter/twitter_loggins.pkl", "wb"))

我收到了以下追溯错误。

Traceback (most recent call last):
  File "save_cookies.py", line 40, in <module>
    cookies1 = pickle.load(loggins)
  File "/usr/lib/python2.7/pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 880, in load_eof
    raise EOFError
EOFError

所以我需要腌制的文件看起来像[['email1','cookie1','cookie2'],['email2','cookie1','cookie2']]

1 个答案:

答案 0 :(得分:0)

您在cookies1 = driver.get_cookies的第一个脚本中有一个错误,您只需引用该方法但不要调用它(它必须是cookies1 = driver.get_cookies())。您打开文件"wb",它会截断文件,但由于函数对象不可选,因此会引发异常并且您有一个空文件。在将来的迭代中,pickle.load失败,因为文件为空。