本文介绍了如何将mechanize.Browser()cookie保存到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使Python的模块机械化(特别是mechanize.Browser())以将其当前的cookie保存到人类可读的文件中?另外,我将如何使用该Cookie将其上传到网页?

How could I make Python's module mechanize (specifically mechanize.Browser()) to save its current cookies to a human-readable file? Also, how would I go about uploading that cookie to a web page with it?

谢谢

推荐答案

Deusdies,我刚刚想出了一种对Mykola Kharechko的

Deusdies,I just figured out a way with refrence to Mykola Kharechko's post

#to save cookie
>>>cookiefile=open('cookie','w')
>>>cookiestr=''
>>>for c in br._ua_handlers['_cookies'].cookiejar:
>>>    cookiestr+=c.name+'='+c.value+';'
>>>cookiefile.write(cookiestr)
#binding this cookie to another Browser
>>>while len(cookiestr)!=0:
>>>    br1.set_cookie(cookiestr)
>>>    cookiestr=cookiestr[cookiestr.find(';')+1:]
>>>cookiefile.close()

这篇关于如何将mechanize.Browser()cookie保存到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:41