def getCookie(): """ Get cookie of Leetcode session from Chrome or Firefox """ cj = browsercookie.load() sessionCSRF = '' sessionID = '' for cookie in cj: if cookie.domain == 'leetcode.com' and cookie.name == 'csrftoken': sessionCSRF = cookie.value if cookie.domain == '.leetcode.com' and cookie.name == 'LEETCODE_SESSION': sessionID = cookie.value if not sessionCSRF or not sessionID: print('ERROR: Cannot find Leetcode cookies.') print( 'Are you sure you are logged into leetcode in Chrome or Firefox?') return with open(PATH + "config.json", "r") as jsonFile: data = json.load(jsonFile) data["LEETCODE_COOKIE"]["sessionCSRF"] = sessionCSRF data["LEETCODE_COOKIE"]["sessionID"] = sessionID with open(PATH + "config.json", "w") as jsonFile: json.dump(data, jsonFile) print('Leetcode cookies saved.')
def get_cookies_by_domain(domain, lazy): domains = domain.find(',') > 0 and domain.split(',') or [domain] cookies = [] for domain in domains: cookies += [i for i in browsercookie.load() if i.domain == domain and i.value or\ lazy and i.domain.find(domain)>0 and i.value] return cookies
def main(): parser = argparse.ArgumentParser(description='Browser cookie inspector') parser.add_argument( "-d", "--domain", help="Print only cookies with matching domain. " "If first char is ~, then treat as regexp.", ) parser.add_argument( "-n", "--name", help="Print only cookies with matching name. " "If first char is ~, then treat as regexp.", ) parser.add_argument( "--path", help="Print only cookies with matching path. " "If first char is ~, then treat as regexp.", ) parser.add_argument( "--value", help="Print only cookies with matching value. " "If first char is ~, then treat as regexp.", ) parser.add_argument( "--ignore-expires", action='store_true', help="Print matching cookies even if they're expired", ) parser.add_argument( "--format", choices=['netscape', 'lwp', 'json', 'value-only'], default='netscape', help="Print matching cookies even if they're expired", ) args = parser.parse_args() cookiejar = browsercookie.load() if args.format == 'lwp': out_cookiejar = cookielib.LWPCookieJar() else: out_cookiejar = cookielib.MozillaCookieJar() filter_cookiejar(cookiejar, out_cookiejar, args.domain, args.name, args.path, args.value) if args.format == 'json': print( json.dumps([cookie.__dict__ for cookie in out_cookiejar], indent=4)) elif args.format in ('netscape', 'lwp'): out_cookiejar.save(filename='/dev/stdout', ignore_expires=args.ignore_expires) elif args.format == 'value-only': for cookie in out_cookiejar: print(cookie.value)
def load_cookie(self): """load cookies from file or browser.""" cookies = [ i for i in browsercookie.load() if i.domain.find("xueqiu") > 0 ] # load cookies from file if os.path.exists(api.cookie_file): sess.cookies.load(ignore_discard=True, ignore_expires=True) self.logined = True # load cookies from browser elif cookies and 'xq_is_login' in [i.name for i in cookies]: [sess.cookies.set_cookie(ck) for ck in cookies] self.logined = True return self.logined
def get_apple_cookie(as_string=False): """ Extracts necessary cookies to retrieve Apple API endpoints from the local user's browsers. This method requires authentication through keychain, to gain access to the browser cookies. """ # Check live cache first global APPLE_COOKIES_CACHE if APPLE_COOKIES_CACHE == None: # Grab cookie jar from all local browsers (will invoke keychain) # (extra surrounding code to avoid msgs module writes carelessly to stdout) devnull = open(os.devnull, 'w') old_stdout = sys.stdout sys.stdout = devnull full_cj = browsercookie.load() sys.stdout = old_stdout # Retrieve the Apple cookies apple_cj = [ cookie for cookie in full_cj if cookie.name in APPLE_COOKIES ] # Update cache APPLE_COOKIES_CACHE = apple_cj else: apple_cj = APPLE_COOKIES_CACHE # Return as cookie list or string if as_string: return "; ".join( map(lambda c: "{}={}".format(c.name, c.value), apple_cj)) return apple_cj
urlgroup = read_pic_url(bs) download_pic(urlgroup, path) if "&page=" in url and int(url[-1]) < maxpagenumber: print("Ready to download page No.", int(url[-1]) + 1) read_topic_and_download(url[:-1] + str(int(url[-1]) + 1), path) else: print("All pages download COMPLETED") return if maxpagenumber > 1: print("Ready to download page No.2") read_topic_and_download(url + "&page=2", path) if __name__ == "__main__": try: cookie = browsercookie.load() except browsercookie.BrowserCookieError: print("No supported browser found (Chrome or Firefox)") time.sleep(300) sys.exit(-1) while True: counter += 1 print("\nThe No.%d loop" % counter) response = requests.get("https://bbs.nga.cn/thread.php?fid=-7", cookies=cookie) if not response.ok: print("Cookie invalid") time.sleep(300) continue
def cookies_test3(): get_title = lambda html: re.findall('<title>(.*?)</title>', html, flags=re.DOTALL)[0].strip() cj = browsercookie.load() # 获取 all available browser cookies r = requests.get(URL, cookies=cj) print get_title(r.content)
import logicoma @logicoma.crawler() def crawler(urls): return urls # To do something eg. on the facebook we need to be logged in... @crawler.handler(r'//www\.facebook\.com/') def facebook(client, url): pass if __name__ == '__main__': # Create crawler client with cookies loaded from browser. import browsercookie crawler.client = logicoma.Client(cookies=browsercookie.load()) crawler([])