简体   繁体   English

如何使用python模块“mechanize”使用chrome扩展名“cookies.txt export”导出的cookies.txt登录网站?

[英]How to login a website using python module “mechanize” with a cookies.txt exported by the chrome extension “cookies.txt export”?

I have looked through similar questions in stackoverflow yet seemingly no answers measures up. 我在stackoverflow中查看了类似的问题,但似乎没有答案可以衡量。 Now I have a cookies.txt in hands which is exported by a chrome extension named "cookies.txt". 现在我手中有一个cookies.txt,它由一个名为“cookies.txt”的chrome扩展名导出。 I can execute a command "wget --load-cookies cookies.txt www.example.com" to download the webpage with the account authenticated. 我可以执行命令“wget --load-cookies cookies.txt www.example.com”下载经过身份验证的帐户的网页。

However, I met a problem when I tried to use this file in my python script as follows, 但是,当我尝试在我的python脚本中使用此文件时遇到了一个问题,如下所示,

import mechanize

cookie = 'cookies.txt'

cookiejar = mechanize.FileCookieJar(cookies.txt)

br = mechanize.Browser()

br.set_handle_robots(False)

br.set_cookiejar(cookiejar)

url = 'www.example.com'
response = br.open(url)
s = response.read()

f = open('test.html','w')
f.write(s)
f.close()

I only got a webpage without my account logged in after executing this script. 执行此脚本后,我只有一个没有登录帐户的网页。 And If I change the first several lines of code into this 如果我将前几行代码改为此代码

import mechanize

cookie = 'cookies.txt'
cookiejar = mechanize.MozillaCookieJar()
cookiejar.load(cookie)

I got an error message "mechanize._clientcookie.LoadError: cookies.txt does not look like a Netscape format cookies file" executing the script. 我收到错误消息“mechanize._clientcookie.LoadError:cookies.txt看起来不像Netscape格式的cookie文件”执行脚本。

I have no idea how I can get the authentication done with this cookies.txt given that this file works in wget command. 鉴于此文件在wget命令中有效,我不知道如何使用此cookies.txt完成身份验证。

I was getting the same error until I added this to the top of my cookie file, and now works. 我收到相同的错误,直到我将其添加到我的cookie文件的顶部,现在工作。

# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.

根据机械化源代码,类MozillaCookieJar的load方法将首先搜索正则表达式“#(Netscape)?HTTP Cookie文件”,因此您可以将此行插入导出的cookie.txt的顶部:

# Netscape HTTP Cookie File

Use the cookielib module to handle the cookies. 使用cookielib模块处理cookie。

http://docs.python.org/library/cookielib.html http://docs.python.org/library/cookielib.html

Specifically, you need to read up on how to use FileCookieJar.load() 具体来说,您需要了解如何使用FileCookieJar.load()

That should get you on track. 这应该让你走上正轨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM