def _init_cookies(self): """Populate the session's cookiejar""" if self.cookiedomain is None: return cookies = self.config("cookies") if cookies: if isinstance(cookies, dict): self._update_cookies_dict(cookies, self.cookiedomain) elif isinstance(cookies, str): cookiefile = util.expand_path(cookies) cookiejar = http.cookiejar.MozillaCookieJar() try: cookiejar.load(cookiefile) except OSError as exc: self.log.warning("cookies: %s", exc) else: self._cookiejar.update(cookiejar) self._cookiefile = cookiefile else: self.log.warning( "expected 'dict' or 'str' value for 'cookies' option, " "got '%s' (%s)", cookies.__class__.__name__, cookies) cookies = cloudflare.cookies(self.category) if cookies: domain, cookies = cookies self._update_cookies_dict(cookies, domain)
def main(): cookiejar = FirefoxCookieJar() # cookiejar.load('firefox/cookies.sqlite') cookiejar.load() out_cookiejar = http.cookiejar.MozillaCookieJar() for cookie in cookiejar: out_cookiejar.set_cookie(cookie) out_cookiejar.save(filename='/dev/stdout', ignore_discard=True, ignore_expires=True)
def test4(): cookiejar = http.cookiejar.MozillaCookieJar() cookiejar.load('cookie.txt') handler = urllib.request.HTTPCookieProcessor(cookiejar) opener = urllib.request.build_opener(handler) response = opener.open("http://www.baidu.com") print(response.read())
def _setup_cookie_processor(self, cookie_filename): if not cookie_filename: return None if (os.path.exists(cookie_filename) and not os.path.isfile(cookie_filename)): raise ValueError("%s exists but is no file" % cookie_filename) elif not os.path.exists(cookie_filename): open(cookie_filename, 'w').close() cookiejar = http.cookiejar.LWPCookieJar(cookie_filename) cookiejar.load(ignore_discard=True) return urllib.request.HTTPCookieProcessor(cookiejar)
def startCourseImporter(): downloadDirectory = MediaManager(mw.col, None).dir() cookiefilename = os.path.join(mw.pm.profileFolder(), 'memrise.cookies') cookiejar = http.cookiejar.MozillaCookieJar(cookiefilename) if os.path.isfile(cookiefilename): cookiejar.load() memriseService = memrise.Service(downloadDirectory, cookiejar) if memriseService.isLoggedIn() or MemriseLoginDialog.login(memriseService): cookiejar.save() memriseCourseImporter = MemriseImportDialog(memriseService) memriseCourseImporter.exec_()
def init_cookies(): cookiejar = http.cookiejar.MozillaCookieJar('_swordfishpds_cookies.txt') try: cookiejar.load() except (FileNotFoundError, http.cookiejar.LoadError): pass atexit.register(cookiejar.save) opener = urllib.request.build_opener() opener.add_handler(urllib.request.HTTPCookieProcessor(cookiejar)) urllib.request.install_opener(opener)
def load_cookie(): file = 'cookie.txt' cookiejar = http.cookiejar.LWPCookieJar() cookiejar.load(filename=file, ignore_expires=True, ignore_discard=True) cookie_processor = urllib.request.HTTPCookieProcessor(cookiejar) opener = urllib.request.build_opener(cookie_processor) response = opener.open('http://www.baidu.com') return response
def LoadCookieFromFileDemo(): filename = 'cookie.txt' cookiejar = http.cookiejar.MozillaCookieJar(filename) cookiejar.load(filename, ignore_discard=True, ignore_expires=True) handler = urllib.request.HTTPCookieProcessor(cookiejar) opener = urllib.request.build_opener(handler) response = opener.open('http://www.baidu.com') print(response.read()) print(cookiejar)
def login(): if not os.path.exists(r"cookie.txt"): PixivID = input("ID:") password = input("Password:"******"pixiv_id"] = PixivID post_data["pass"] = password request = urllib.request.Request('http://www.pixiv.net/login.php', urllib.parse.urlencode(post_data).encode(encoding='utf_8')) urlOpener.open(request) cookiejar.save("cookie.txt") else: cookiejar.load("cookie.txt")
def _init_cookies(self): """Populate the session's cookiejar""" cookies = self.config("cookies") if cookies: if isinstance(cookies, dict): self._update_cookies_dict(cookies, self.cookiedomain) else: cookiejar = http.cookiejar.MozillaCookieJar() try: cookiejar.load(cookies) except OSError as exc: self.log.warning("cookies: %s", exc) else: self.session.cookies.update(cookiejar) cookies = cloudflare.cookies(self.category) if cookies: domain, cookies = cookies self._update_cookies_dict(cookies, domain)
"Accept-Encoding": accept_encoding, "Connection": connection, "Referer": referer } headers_list_tuple = list(headers_map.items()) ##代理到本地的fiddler,拦截,查看http的请求的头部 proxy_table = {"http": "127.0.0.1:8888"} ## 处理cookie的handler current_dir = os.getcwd() + os.sep cookie_filename = "cookie.txt" cookie_file_path = current_dir + cookie_filename cookiejar = http.cookiejar.LWPCookieJar(cookie_file_path) cookiejar.load(cookie_file_path, ignore_discard=True, ignore_expires=True) cookiejar.load(filename="./cookie.txt") # print(cookiejar) cookie_handler = urllib.request.HTTPCookieProcessor(cookiejar) proxy_handler = urllib.request.ProxyHandler(proxy_table) ## 为opener注册handler; 十分类似node/express的中间件 body-parser cookie-parser opener = urllib.request.build_opener(proxy_handler, cookie_handler) ## 为opener添加请求的头部 opener.addheaders = headers_list_tuple ## opener.addheaders = [(key,value), (key1, value1)] ## opener.addheaders.append(('Cookie', 'cookiename=cookievalue')) ## Request.add_header(key, val) request = urllib.request.Request(url2) ## 添加method ## Request(url, data, headers=headers, method=method)
┗━┓ ┏━┛ ┃ ┗━━━┓ ┃ 神兽保佑 ┣┓ ┃ 永无BUG! ┏┛ ┗┓┓┏━┳┓┏┛ ┃┫┫ ┃┫┫ ┗┻┛ ┗┻┛ """ from urllib import request import http.cookiejar import chardet # 创建 MozillaCookieJar(有 load 实现)实例对象 cookiejar = http.cookiejar.MozillaCookieJar() # 从文件中读取 cookie 内容到变量 cookiejar.load('./data/cookie.txt') # 使用 HTTPCookieProcessor()来创建 cookie 处理器对象,参数为 CookieJar()对象 handler = request.HTTPCookieProcessor(cookiejar) #通过 build_opener() 来构建 opener opener = request.build_opener(handler) response = opener.open("https://www.baidu.com") # 4. 打印响应内容 html = response.read() charset = chardet.detect(html)['encoding'] print(charset) print(html.decode(charset))
import io import re import requests import http.cookiejar from bs4 import BeautifulSoup import html import csv import time import multiprocessing cookiejar = http.cookiejar.MozillaCookieJar('cookies.txt') cookiejar.load() retry_strategy = requests.packages.urllib3.util.retry.Retry(total=3) http_adapter = requests.adapters.HTTPAdapter(max_retries=retry_strategy) http_session = requests.Session() http_session.mount("https://", http_adapter) http_session.mount("http://", http_adapter) http_session.cookies = cookiejar def read_url(url): r = http_session.get(url) return r.text def
def auto_login(): cookiejar.load() html = openurl('http://weibo.com/', 'gbk') url = match_login_url(html) opener.open(url) """
# coding = utf-8 ''' @author = super_fazai @File : cookielib_demo3.py @Time : 2017/8/28 16:24 @connect : [email protected] ''' """ 从文件中获取cookies,作为请求的一部分去访问 """ from http import cookiejar import urllib.request # 创建MozillaCookieJar(有load实现)实例对象 cookiejar = cookiejar.MozillaCookieJar() # 从文件中读取cookie内容到变量 cookiejar.load('cookie.txt') handler = urllib.request.HTTPCookieProcessor(cookiejar) opener = urllib.request.build_opener(handler) response = opener.open('http://www.baidu.com') print(response.read().decode())
def auto_login(): cookiejar.load() html=openurl('http://weibo.com/','gbk') url=match_login_url(html) opener.open(url) """