0

How can I get cookies of a website from browser using python? The code currently being used is:

get_title = lambda html: re.findall('<title>(.*?)</title>', html, flags=re.DOTALL)[0].strip()
url = config.base_url
public_html = urllib2.urlopen(url).read()
print get_title(public_html)
cj = browsercookie.firefox()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_html = opener.open(url).read()
print get_title(login_html)

This code comes after the application has logged in. config.base_url = "https://10.194.13.71" It is giving this error : c** File "/root/Desktop/mysonicwallnew/testservice.py", line 26, in test_service public_html = urllib2.urlopen(url).read()

CertificateError: hostname '10.194.31.71' doesn't match either of 'www.abc.com', 'abc.com' ** How do I fix this?

Shruti Agarwal
  • 887
  • 2
  • 14
  • 29

1 Answers1

0

This works for me -

import requests
import browsercookie
import re

cj = browsercookie.chrome()
r = requests.get('http://stackoverflow.com', cookies=cj)

get_title = lambda html: re.findall('<title>(.*?)</title>', html, flags=re.DOTALL)[0].strip()
print r.content
print get_title(r.content)

Try updating question with the error you are facing or the exact thing you are looking for from the cookie to get more specific answers.

Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
  • The browser I am running it on is firefox..I am actually running a testcase on selenium webdriver..so in middle of the testcase I have to check whether the person is logged in or not.. – Shruti Agarwal Nov 28 '16 at 06:37
  • I changed the code to cj = browsercookie.firefox() and got this error : **Traceback (most recent call last): File "/root/Desktop/mysonicwallnew/testservice.py", line 24, in test_service cj = browsercookie.firefox() File "/usr/local/lib/python2.7/site-packages/browsercookie/__init__.py", line 207, in firefox return Firefox(cookie_file).load() File "/usr/local/lib/python2.7/site-packages/browsercookie/__init__.py", line 167, in load cur.execute('select host, path, isSecure, expiry, name, value from moz_cookies') DatabaseError: file is encrypted or is not a database ** – Shruti Agarwal Nov 28 '16 at 07:32