使用 Python 从 Cookie Jar 中提取特定的 Cookie
extracting specific Cookies from the Cookie Jar using Python
我正在尝试使用 python browser_cookie3 模块从 chrome 提取特定站点的 cookie。站点 Name:nseindia.com
此(工作)代码已交给我们,但变得相当慢,因为需要磁盘读写。还有一些依赖关系,代码不是很有效。
代码片段:
import browser_cookie3, csv, re
cj = browser_cookie3.chrome()
cookielist = list(browser_cookie3.chrome())
cookies = str(cj)
CookieFile = open('c:\temp\temp2.txt','w+')
CookieFile.write(cookies)
CookieFile.close()
with open('c:\temp\temp2.txt', newline='') as f:
reader = csv.reader(f)
cookies_list = next(reader)
#this traverses thru each item in the list and displays the cookie as well as the value.
for elem in cookies_list:
mycook = re.search(r'for .nseindia.com/>',elem)
if mycook:
thiselem = str(elem)
print(re.search(r'<Cookie (.+?)for \.nseindia\.com', thiselem).group(1))
现在,我已经尝试使用 re.search 提取所有 6 个项目(来自 nseindia.com),但没有给我想要的结果。
以下是我已经尝试过的:
cookies = str(cj)
re.search('<Cookie RT=(.*?)for .nseindia.com',cookies).group(1)
另外我们也试过把cookie对象改成一个列表再提取,都失败了。
当我们尝试将字符串对象 (varname = cookie) 转换为列表,然后尝试使用 re.search 进行提取时,同样失败了。
我的问题:如果有人能给我一个可靠的方法,以有效和高效的方式实现上述功能,我们将不胜感激。 TIA
我找到了一种无需磁盘读写即可实现结果的方法,
import browser_cookie3,re
cj = browser_cookie3.chrome()
cookies = str(cj)
cookielist = cookies.split(",")
for elem in cookielist:
mycook = re.search(r'for .nseindia.com/>',elem)
if mycook:
thiselem = str(elem)
print(re.search(r'<Cookie (.+?)for \.nseindia\.com', thiselem).group(1))
您可以使用
import browser_cookie3,re
cj = browser_cookie3.chrome()
results = re.findall(r'<Cookie\s([^<>]+?)\sfor\s\.nseindia\.com', str(cj))
如果要打印并显示所有内容,请使用 print(results)
或 for r in results: print(r)
。
见regex demo。 详情:
<Cookie
- <Cookie
字符串
\s
- 一个空格
([^<>]+?)
- 第 1 组:<~ and
>` 以外的任何一个或多个字符尽可能少
\s
- 一个空格
for
- for
\s
- 一个空格
\.nseindia\.com
- .nseindia.com
字符串。
我正在尝试使用 python browser_cookie3 模块从 chrome 提取特定站点的 cookie。站点 Name:nseindia.com
此(工作)代码已交给我们,但变得相当慢,因为需要磁盘读写。还有一些依赖关系,代码不是很有效。
代码片段:
import browser_cookie3, csv, re
cj = browser_cookie3.chrome()
cookielist = list(browser_cookie3.chrome())
cookies = str(cj)
CookieFile = open('c:\temp\temp2.txt','w+')
CookieFile.write(cookies)
CookieFile.close()
with open('c:\temp\temp2.txt', newline='') as f:
reader = csv.reader(f)
cookies_list = next(reader)
#this traverses thru each item in the list and displays the cookie as well as the value.
for elem in cookies_list:
mycook = re.search(r'for .nseindia.com/>',elem)
if mycook:
thiselem = str(elem)
print(re.search(r'<Cookie (.+?)for \.nseindia\.com', thiselem).group(1))
现在,我已经尝试使用 re.search 提取所有 6 个项目(来自 nseindia.com),但没有给我想要的结果。
以下是我已经尝试过的:
cookies = str(cj)
re.search('<Cookie RT=(.*?)for .nseindia.com',cookies).group(1)
另外我们也试过把cookie对象改成一个列表再提取,都失败了。 当我们尝试将字符串对象 (varname = cookie) 转换为列表,然后尝试使用 re.search 进行提取时,同样失败了。
我的问题:如果有人能给我一个可靠的方法,以有效和高效的方式实现上述功能,我们将不胜感激。 TIA
我找到了一种无需磁盘读写即可实现结果的方法,
import browser_cookie3,re
cj = browser_cookie3.chrome()
cookies = str(cj)
cookielist = cookies.split(",")
for elem in cookielist:
mycook = re.search(r'for .nseindia.com/>',elem)
if mycook:
thiselem = str(elem)
print(re.search(r'<Cookie (.+?)for \.nseindia\.com', thiselem).group(1))
您可以使用
import browser_cookie3,re
cj = browser_cookie3.chrome()
results = re.findall(r'<Cookie\s([^<>]+?)\sfor\s\.nseindia\.com', str(cj))
如果要打印并显示所有内容,请使用 print(results)
或 for r in results: print(r)
。
见regex demo。 详情:
<Cookie
-<Cookie
字符串\s
- 一个空格([^<>]+?)
- 第 1 组:<~ and
>` 以外的任何一个或多个字符尽可能少\s
- 一个空格for
-for
\s
- 一个空格\.nseindia\.com
-.nseindia.com
字符串。