本文整理汇总了Python中http.cookiejar.MozillaCookieJar类的典型用法代码示例。如果您正苦于以下问题:Python MozillaCookieJar类的具体用法?Python MozillaCookieJar怎么用?Python MozillaCookieJar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MozillaCookieJar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_cookies_from_ff
def get_cookies_from_ff(db_filename):
con = sqlite.connect(db_filename)
con.execute("pragma journal_mode=WAL")
cur = con.cursor()
cur.execute(
"select host, path, isSecure, expiry, name, value from moz_cookies"
)
container = []
while True:
try:
row = cur.fetchone()
except:
continue
if not row:
break
if not row[4].startswith('chkSlider'): # FIXME: this is a dirty fix
container.append(row)
con.close()
ftstr = ["FALSE", "TRUE"]
s = StringIO()
s.write("""\
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file! Do not edit.
""")
for item in container:
v = "%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (
item[0], ftstr[item[0].startswith('.')], item[1],
ftstr[item[2]], item[3], item[4], item[5])
s.write(v)
s.seek(0)
cookie_jar = MozillaCookieJar()
cookie_jar._really_load(s, '', True, True)
return cookie_jar
示例2: __init__
def __init__(self, mobile, password=None, status='0',
cachefile='Fetion.cache', cookiesfile=''):
'''登录状态:
在线:400 隐身:0 忙碌:600 离开:100
'''
if cachefile:
self.cache = Cache(cachefile)
if not cookiesfile:
cookiesfile = '%s.cookies' % mobile
cookiejar = MozillaCookieJar(filename=cookiesfile)
if not os.path.isfile(cookiesfile):
open(cookiesfile, 'w').write(MozillaCookieJar.header)
cookiejar.load(filename=cookiesfile)
cookie_processor = HTTPCookieProcessor(cookiejar)
self.opener = build_opener(cookie_processor,
HTTPHandler)
self.mobile, self.password = mobile, password
if not self.alive():
self._login()
cookiejar.save()
self.changestatus(status)
示例3: have_cookie_login
def have_cookie_login(self):
print('Test cookies...')
cookie = MozillaCookieJar()
cookie.load(self.cookiesFile, ignore_discard=True, ignore_expires=True)
self.build_opener(cookie, self.use_proxy)
page = self.get_page_data(self.userSetUrl)
if not search('page-setting-user', page):
print('This cookies has been invalid.')
remove(self.cookiesFile)
self.have_not_cookie_login()
示例4: ScholarQuerier
class ScholarQuerier(object):
"""
ScholarQuerier instances can conduct a search on Google Scholar
with subsequent parsing of the resulting HTML content. The
articles found are collected in the articles member, a list of
ScholarArticle instances.
"""
# Default URLs for visiting and submitting Settings pane, as of 3/14
GET_SETTINGS_URL = ScholarConf.SCHOLAR_SITE + '/scholar_settings?' \
+ 'sciifh=1&hl=en&as_sdt=0,5'
SET_SETTINGS_URL = ScholarConf.SCHOLAR_SITE + '/scholar_setprefs?' \
+ 'q=' \
+ '&scisig=%(scisig)s' \
+ '&inststart=0' \
+ '&as_sdt=1,5' \
+ '&as_sdtp=' \
+ '&num=%(num)s' \
+ '&scis=%(scis)s' \
+ '%(scisf)s' \
+ '&hl=en&lang=all&instq=&inst=569367360547434339&save='
# Older URLs:
# ScholarConf.SCHOLAR_SITE + '/scholar?q=%s&hl=en&btnG=Search&as_sdt=2001&as_sdtp=on
class Parser(ScholarArticleParser120726):
def __init__(self, querier):
ScholarArticleParser120726.__init__(self)
self.querier = querier
def handle_article(self, art):
self.querier.add_article(art)
def __init__(self):
self.articles = []
self.query = None
self.cjar = MozillaCookieJar()
# If we have a cookie file, load it:
if ScholarConf.COOKIE_JAR_FILE and \
os.path.exists(ScholarConf.COOKIE_JAR_FILE):
try:
self.cjar.load(ScholarConf.COOKIE_JAR_FILE,
ignore_discard=True)
ScholarUtils.log('info', 'loaded cookies file')
except Exception,msg:
ScholarUtils.log('warn', 'could not load cookies file: %s' % msg)
self.cjar = MozillaCookieJar() # Just to be safe
self.opener = build_opener(HTTPCookieProcessor(self.cjar))
self.settings = None # Last settings object, if any
示例5: __init__
def __init__(self):
self.articles = []
self.query = None
self.cjar = MozillaCookieJar()
# If we have a cookie file, load it:
if ScholarConf.COOKIE_JAR_FILE and \
os.path.exists(ScholarConf.COOKIE_JAR_FILE):
try:
self.cjar.load(ScholarConf.COOKIE_JAR_FILE,
ignore_discard=True)
ScholarUtils.log('info', 'loaded cookies file')
except Exception,msg:
ScholarUtils.log('warn', 'could not load cookies file: %s' % msg)
self.cjar = MozillaCookieJar() # Just to be safe
示例6: __init__
def __init__(self):
self.articles = []
self.query = None
self.cjar = MozillaCookieJar()
# If we have a cookie file, load it:
if ScholarConf.COOKIE_JAR_FILE and os.path.exists(ScholarConf.COOKIE_JAR_FILE):
try:
self.cjar.load(ScholarConf.COOKIE_JAR_FILE, ignore_discard=True)
ScholarUtils.log("info", "loaded cookies file")
except Exception as msg:
ScholarUtils.log("warn", "could not load cookies file: %s" % msg)
self.cjar = MozillaCookieJar() # Just to be safe
self.opener = build_opener(HTTPCookieProcessor(self.cjar))
self.settings = None # Last settings object, if any
示例7: __init__
def __init__(
self,
cookiejar_path=None,
cookiejar=None,
token=None,
categories=None
):
"""
cookiejar: a MozillaCookieJar object
token: a user token for submitting form data
categories: package categories
"""
if cookiejar_path is None:
cookiejar_path = get_default_cookiejar_path()
self.cookiejar_path = cookiejar_path
if cookiejar is None:
self.cookiejar = MozillaCookieJar()
self.load_cookies()
else:
self.cookiejar = cookiejar
# TODO
# Find way to use this with URL opener. (urlopen accepts a capath arg)
# CA_PATH = '/etc/ssl/certs'
self.opener = build_opener(HTTPCookieProcessor(self.cookiejar))
self.token = token
self.categories = categories
# self.rpc = AUR(ttl=0, clean=False)
self.rpc = AUR()
示例8: __init__
def __init__(self):
self.articles = []
self.query = None
self.cjar = MozillaCookieJar()
# If we have a cookie file, load it:
if ScholarConf.COOKIE_JAR_FILE and \
os.path.exists(ScholarConf.COOKIE_JAR_FILE):
try:
self.cjar.load(ScholarConf.COOKIE_JAR_FILE,
ignore_discard=True)
ScholarUtils.log('info', 'loaded cookies file')
except Exception as msg:
ScholarUtils.log('warn', 'could not load cookies file: %s' % msg)
self.cjar = MozillaCookieJar() # Just to be safe
# proxy = ProxyHandler({'http': '111.246.31.239:8888'})
# self.opener = build_opener(HTTPCookieProcessor(self.cjar), proxy)
self.opener = build_opener(HTTPCookieProcessor(self.cjar))
self.settings = None # Last settings object, if any
示例9: Session
class Session(object):
def __init__(self, app_name, app_version, data_path, **kwargs):
self.app_name = app_name
self.app_version = app_version
if not data_path or not os.path.isdir(data_path):
raise Exception('invalid data_path: %s' % data_path)
self.cookie_jar = MozillaCookieJar(os.path.join(data_path, default.COOKIES_FILENAME))
try:
self.cookie_jar.load()
except EnvironmentError:
pass
self.opener = build_opener(
HTTPRedirectHandler(),
HTTPCookieProcessor(self.cookie_jar))
super(Session, self).__init__(**kwargs)
def open(self, request, default_charset=None):
request.add_header('User-Agent', util.user_agent(self.app_name, self.app_version))
system_string = json.dumps(util.system_info(self.app_name, self.app_version))
request.add_header('X-Sputnik-System', system_string)
r = self.opener.open(request)
if hasattr(r.headers, 'get_content_charset'): # py3
charset = r.headers.get_content_charset() or default_charset
elif hasattr(r.headers, 'getparam'): # py2
charset = r.headers.getparam('charset') or default_charset
else:
charset = default_charset
if charset is None:
return r
return codecs.getreader(charset)(r)
def __del__(self):
if hasattr(self, 'cookie_jar'):
self.cookie_jar.save()
示例10: login
def login(self, pixiv_id='614634238', password='spider614634238'):
cookie = MozillaCookieJar(self.cookiesFile)
self.build_opener(cookie, self.use_proxy)
print('Login...')
page = self.get_page_data(self.loginUrl)
pattern = compile('name="post_key"\s*value="(.*?)"')
post_key = search(pattern, page).group(1)
post_value = {
'pixiv_id': pixiv_id,
'password': password,
'g_recaptcha_response': '',
'post_key': post_key,
'source': 'pc'
}
page = self.get_page_data(self.loginUrl, post_value)
if search('error-msg-list', page):
print('Login failed.')
raise SystemExit(1)
cookie.save(ignore_discard=True, ignore_expires=True)
示例11: Session
class Session(Base):
def __init__(self, data_path, **kwargs):
if not validation.is_data_path(data_path):
raise Exception('invalid data_path: %s' % data_path)
self.cookie_jar = MozillaCookieJar(os.path.join(data_path, default.COOKIES_FILENAME))
try:
self.cookie_jar.load()
except EnvironmentError:
pass
self.opener = build_opener(
HTTPRedirectHandler(),
HTTPCookieProcessor(self.cookie_jar))
super(Session, self).__init__(**kwargs)
def open(self, request, default_charset=None):
request.add_header('User-Agent', self.s.user_agent())
if self.s.name:
request.add_header('X-Sputnik-Name', self.s.name)
if self.s.version:
request.add_header('X-Sputnik-Version', self.s.version)
r = self.opener.open(request)
if hasattr(r.headers, 'get_content_charset'): # py3
charset = r.headers.get_content_charset() or default_charset
elif hasattr(r.headers, 'getparam'): # py2
charset = r.headers.getparam('charset') or default_charset
else:
charset = default_charset
if charset is None:
return r
return codecs.getreader(charset)(r)
def __del__(self):
if hasattr(self, 'cookie_jar'):
self.cookie_jar.save()
示例12: setCookieJar
def setCookieJar(self,cookiejar):
"""Changes the CookieJar used to manage the session.
Existing cookies will not be transferred.
Returns: the old CookieJar"""
tmpcookies = getattr(self,"cookies",None)
if type(cookiejar) == str:
self.cookies = MozillaCookieJar(cookiejar)
if os.path.exists(cookiejar):
self.cookies.load(ignore_discard=True)
else:
self.cookies = cookiejar
self.opener = build_opener(HTTPCookieProcessor(self.cookies))
return tmpcookies
示例13: __init__
def __init__(self, data_path, **kwargs):
if not validation.is_data_path(data_path):
raise Exception('invalid data_path: %s' % data_path)
self.cookie_jar = MozillaCookieJar(os.path.join(data_path, default.COOKIES_FILENAME))
try:
self.cookie_jar.load()
except EnvironmentError:
pass
self.opener = build_opener(
HTTPRedirectHandler(),
HTTPCookieProcessor(self.cookie_jar))
super(Session, self).__init__(**kwargs)
示例14: ScholarQuerier
class ScholarQuerier(object):
"""
ScholarQuerier instances can conduct a search on Google Scholar
with subsequent parsing of the resulting HTML content. The
articles found are collected in the articles member, a list of
ScholarArticle instances.
"""
# Default URLs for visiting and submitting Settings pane, as of 3/14
GET_SETTINGS_URL = ScholarConf.SCHOLAR_SITE + '/scholar_settings?' \
+ 'sciifh=1&hl=en&as_sdt=0,5'
SET_SETTINGS_URL = ScholarConf.SCHOLAR_SITE + '/scholar_setprefs?' \
+ 'q=' \
+ '&scisig=%(scisig)s' \
+ '&inststart=0' \
+ '&as_sdt=1,5' \
+ '&as_sdtp=' \
+ '&num=%(num)s' \
+ '&scis=%(scis)s' \
+ '%(scisf)s' \
+ '&hl=en&lang=all&instq=&inst=569367360547434339&save='
# Older URLs:
# ScholarConf.SCHOLAR_SITE + '/scholar?q=%s&hl=en&btnG=Search&as_sdt=2001&as_sdtp=on
class Parser(ScholarArticleParser120726):
def __init__(self, querier):
ScholarArticleParser120726.__init__(self)
self.querier = querier
def handle_article(self, art):
self.querier.add_article(art)
def __init__(self):
self.articles = []
self.query = None
self.cjar = MozillaCookieJar()
# If we have a cookie file, load it:
if ScholarConf.COOKIE_JAR_FILE and \
os.path.exists(ScholarConf.COOKIE_JAR_FILE):
try:
self.cjar.load(ScholarConf.COOKIE_JAR_FILE,
ignore_discard=True)
ScholarUtils.log('info', 'loaded cookies file')
except Exception as msg:
ScholarUtils.log('warn', 'could not load cookies file: %s' % msg)
self.cjar = MozillaCookieJar() # Just to be safe
self.opener = build_opener(HTTPCookieProcessor(self.cjar))
self.settings = None # Last settings object, if any
def apply_settings(self, settings):
"""
Applies settings as provided by a ScholarSettings instance.
"""
if settings is None or not settings.is_configured():
return True
self.settings = settings
# This is a bit of work. We need to actually retrieve the
# contents of the Settings pane HTML in order to extract
# hidden fields before we can compose the query for updating
# the settings.
html = self._get_http_response(url=self.GET_SETTINGS_URL,
log_msg='dump of settings form HTML',
err_msg='requesting settings failed')
if html is None:
return False
# Now parse the required stuff out of the form. We require the
# "scisig" token to make the upload of our settings acceptable
# to Google.
soup = BeautifulSoup(html)
tag = soup.find(name='form', attrs={'id': 'gs_settings_form'})
if tag is None:
ScholarUtils.log('info', 'parsing settings failed: no form')
return False
tag = tag.find('input', attrs={'type':'hidden', 'name':'scisig'})
if tag is None:
ScholarUtils.log('info', 'parsing settings failed: scisig')
return False
urlargs = {'scisig': tag['value'],
'num': settings.per_page_results,
'scis': 'no',
'scisf': ''}
if settings.citform != 0:
urlargs['scis'] = 'yes'
urlargs['scisf'] = '&scisf=%d' % settings.citform
html = self._get_http_response(url=self.SET_SETTINGS_URL % urlargs,
log_msg='dump of settings result HTML',
err_msg='applying setttings failed')
#.........这里部分代码省略.........
示例15: MozillaCookieJar
urls = [
"http://crypto.stackexchange.com/users/593/b-con",
"http://security.stackexchange.com/users/8857/b-con",
"http://stackoverflow.com/users/1361836/b-con"
]
logging.basicConfig(filename="/tmp/site-ping.log",
datefmt="%m-%d %H:%M",
level=logging.DEBUG)
# Extract the cookies from Firefox. The script to do so is co-located.
path = os.path.dirname(os.path.realpath(__file__))
p = subprocess.call(path + "/extract-cookies.sh")
# Load the cookies.
cj = MozillaCookieJar("/tmp/firefox-cookies.txt")
try:
cj.load()
except FileNotFoundErr as ex:
logging.error(ex)
quit(1)
# Use the cookies to visit each of the URLs.
for url in urls:
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
response = opener.open(url)
html = response.read().decode("utf-8")
response.close()
# The "votes" tab only appears on the user profile when you're logged in.