10 examples of 'python get cookie from browser' in Python

Every line of 'python get cookie from browser' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
182def read_session_cookie(http_response):
183 headers = http_response.getheaders()
184 for name, value in headers:
185 if name == 'set-cookie':
186 cookie_name, cookie_value = parse_cookie(value)
187 print cookie_name, cookie_value
188 if cookie_name == 'session_id':
189 return cookie_value
190 return None
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

8def get_lagaou_cookie():
9 browser = webdriver.Chrome(executable_path="D:/mylibs/chromedriver/chromedriver.exe")
10 browser.get("https://passport.lagou.com/login/login.html")
11 browser.find_element_by_xpath("/html/body/section/div[1]/div[2]/form/div[1]/input").send_keys("18875299597")
12 browser.find_element_by_xpath("/html/body/section/div[1]/div[2]/form/div[2]/input").send_keys("5201314xf")
13 browser.find_element_by_xpath("/html/body/section/div[1]/div[2]/form/div[5]/input").click()
14 browser.get("https://www.lagou.com")
15 print(browser.get_cookies())
16 browser.close()
329def get_cookie(name, default=None):
330 return _publisher.get_request().get_cookie(name, default)
41def get_bpc_cookie(self):
42 if 'BPC' not in self.session.cookies:
43 self.home.go()
44 bpcCookie = str(self.page.content).split('BPC=')[-1].split('"')[0]
45 self.session.cookies['BPC'] = bpcCookie
279def load_cookie(self, session_id):
280 sessions = json.load(open( self.sessions_file ))
281 cookie_path = sessions[str(session_id)]["session_path"]
282 url = sessions[str(session_id)]["web_url"]
283 # Setting useragent to the same one the session saved with
284 useragent = sessions[str(session_id)]["useragent"]
285 profile = FirefoxProfile()
286 profile.set_preference("general.useragent.override", useragent )
287 cookies = pickle.load(open(cookie_path, "rb"))
288 try:
289 browser = Firefox(profile)
290 except:
291 error("Couldn't open browser to view session!")
292 return
293 browser.get(url)
294 browser.delete_all_cookies()
295 browser.execute_script("window.localStorage.clear()") # clear the current localStorage
296 for cookie in cookies:
297 browser.add_cookie(cookie)
298 status(f"Session {session_id} loaded")
299 browser.refresh()
300 self.browsers.append(browser)
370def set_cookie(self, cookie_string):
371 """Request to set a cookie.
372
373 Note that it is NOT necessary to call this method under ordinary
374 circumstances: cookie handling is normally entirely automatic. The
375 intended use case is rather to simulate the setting of a cookie by
376 client script in a web page (e.g. JavaScript). In that case, use of
377 this method is necessary because mechanize currently does not support
378 JavaScript, VBScript, etc.
379
380 The cookie is added in the same way as if it had arrived with the
381 current response, as a result of the current request. This means that,
382 for example, if it is not appropriate to set the cookie based on the
383 current request, no cookie will be set.
384
385 The cookie will be returned automatically with subsequent responses
386 made by the Browser instance whenever that's appropriate.
387
388 cookie_string should be a valid value of the Set-Cookie header.
389
390 For example:
391
392 browser.set_cookie(
393 "sid=abcdef; expires=Wednesday, 09-Nov-06 23:12:40 GMT")
394
395 Currently, this method does not allow for adding RFC 2986 cookies.
396 This limitation will be lifted if anybody requests it.
397
398 """
399 if self._response is None:
400 raise BrowserStateError("not viewing any document")
401 if self.request.get_type() not in ["http", "https"]:
402 raise BrowserStateError("can't set cookie for non-HTTP/HTTPS "
403 "transactions")
404 cookiejar = self._ua_handlers["_cookies"].cookiejar
405 response = self.response() # copy
406 headers = response.info()
407 headers["Set-cookie"] = cookie_string
408 cookiejar.extract_cookies(response, self.request)
275def get_cookie_header(self, req):
276 """
277 :param req: object with httplib.Request interface
278 Actually, it have to have `url` and `headers` attributes
279 """
280 mocked_req = MockRequest(req)
281 self.cookiejar.add_cookie_header(mocked_req)
282 return mocked_req.get_new_headers().get('Cookie')
75def getCookie(key):
76 """
77 Get a cookie that was sent from the network.
78
79 @type key: L{bytes}
80 @param key: The name of the cookie to get.
81
82 @rtype: L{bytes} or L{None}
83 @returns: The value of the specified cookie, or L{None} if that cookie
84 was not present in the request.
85 """
43def _get_cookie_data(self, response):
44 return response.cookies.get_dict()
76@property
77def value(self) -> str:
78 """Cookie value"""
79 return self._cookie["value"]

Related snippets