在工作时,我需要获取cookies,先就如何获取cookies的过程做个简单的记录
name:cookie键名称
path:cookie路径
HTTP: 表示是否是http read lonly,打钩代表是
pypeteer框架本身就自带获取cookie的方法
# pypeteer中的方法# 注:iframe对象中无cookies对象cookie = await page.cookies()# 该方法可以获取,该本页面中所有的根目录下的cookie值(即路径为 / 的cookie)# 获取到的cookie示例 未获取到途中对应的JSESSIONID的值,因为其对应的路径为 /shenbao“”“[{'name': 'first-cookie', 'value': '79852665', 'domain': 'etax.jiangsu.', 'path': '/', 'expires': -1, 'size': 20, 'httpOnly': False, 'secure': False, 'session': True}, {'name': 'SSOSESSIONID', 'value': '789AfrQGDwVqVvv4EbA1HpPfzqQ5ol6ImmSbgDJjpE6_g_yZiiGc!2099279361', 'domain': 'etax.jiangsu.', 'path': '/', 'expires': -1, 'size': 75, 'httpOnly': True, 'secure': False, 'session': True}, {'name': 'YD00230621733232%3AWM_TID', 'value': 'LQvI0kJrl%2FBBEVUFEBYrZnABLtOqekL9', 'domain': 'etax.jiangsu.', 'path': '/', 'expires': 2147483647, 'size': 59, 'httpOnly': False, 'secure': False, 'session': False}, {'name': 'cookie', 'value': '17737058', 'domain': 'etax.jiangsu.', 'path': '/', 'expires': -1, 'size': 14, 'httpOnly': False, 'secure': False, 'session': True}, {'name': 'YD00230621733232%3AWM_NI', 'value': 'h1WAK2PJ2Q1BojGJIutuy0lvSvKgUDXcbxuaWkzIhr3rsjUYKwAYYpjdsMYyhfya6BvBUp6Bn9uhtSUW6v%2FCZWpSTJop%2FN2PaWVdaDgq15EN6s%2FlBpr1GlP8T6OY5Fx9WlI%3D', 'domain': 'etax.jiangsu.', 'path': '/', 'expires': 2147483647, 'size': 164, 'httpOnly': False, 'secure': False, 'session': False}, {'name': '_9755xjdesxxd_', 'value': '32', 'domain': 'etax.jiangsu.', 'path': '/', 'expires': 1760785435, 'size': 16, 'httpOnly': False, 'secure': False, 'session': False}, {'name': 'gdxidpyhxdE', 'value': 'hmlWrJvSf330rC5ZJOwD0QZmhXPLj0eHSWOkaI3sX%5C43MwdQyeU6xz%2BgLd8dDkn%2FA21TEUl%2F3bHgKJKI2u6p4YhWfVUdMTRHDxNgs0PC2atSltN9imgSUJtdGQc2vX0t2141V97p0Qmm1MzL9%5CSXTRhMT%2BwESkpf8WPJMHgJNAfiVVId%3A1603105435575', 'domain': 'etax.jiangsu.', 'path': '/', 'expires': 1760785435, 'size': 215, 'httpOnly': False, 'secure': False, 'session': False},{'name': 'YD00230621733232%3AWM_NIKE', 'value': '9ca17ae2e6ffcda170e2e6ee98b16a9ba9a6d0dc5b968a8aa7d54a869e9eaeb570f29a9eadbb6bf5bea9b1bb2af0fea7c3b92aacef99a2ca3a9496feb2b44aadb689abcc7fb8abb7d1ef7e91aa8588d8509cbdae99f86b95b5b6aaeb7af1aebd8bd85389a98fbadc7caabcbdaedc73acbab782b54ab69e9b91b54aaee7bb89f7599789fa97ae499a89c0b4d742adb0e1afb15ca3899a85e450a5afffd6c846f6ade18dc17cb2ef898dd33498ea81bab84ebce7998dcc37e2a3', 'domain': 'etax.jiangsu.', 'path': '/', 'expires': 2147483647, 'size': 396, 'httpOnly': False, 'secure': False, 'session': False}, {'name': 'GDS_PORTAL_NSRSBH', 'value': '9137MA1Y798Y17', 'domain': 'etax.jiangsu.', 'path': '/', 'expires': 1603190987, 'size': 35, 'httpOnly': False, 'secure': False, 'session': False}, {'name': 'JSESSIONID1', 'value': 'iXJAfzjzmvVxSUSJPsBiGNwy9Gcij_ZxsILBJNp31FxYanFMX5qg!1964044017', 'domain': 'etax.jiangsu.', 'path': '/', 'expires': -1, 'size': 74, 'httpOnly': True, 'secure': False, 'session': True}]”“”
可以通过js的方法获取cookie值
以上截图获取的是/目录下的非http read lonely的键值信息
一下就是截图的中具体的cookie信息
first-cookie=79852665; gdxidpyhxdE=hmlWrJvSf330rC5ZJOwD0QZmhXPLj0eHSWOkaI3sX%5C43MwdQyeU6xz%2BgLd8dDkn%2FA21TEUl%2F3bHgKJKI2u6p4YhWfVUdMTRHDxNgs0PC2atSltN9imgSUJtdGQc2vX0t2141V97p0Qmm1MzL9%5CSXTRhMT%2BwESkpf8WPJMHgJNAfiVVId%3A1603105435575; _9755xjdesxxd_=32; YD00230621733232%3AWM_NI=h1WAK2PJ2Q1BojGJIutuy0lvSvKgUDXcbxuaWkzIhr3rsjUYKwAYYpjdsMYyhfya6BvBUp6Bn9uhtSUW6v%2FCZWpSTJop%2FN2PaWVdaDgq15EN6s%2FlBpr1GlP8T6OY5Fx9WlI%3D; YD00230621733232%3AWM_NIKE=9ca17ae2e6ffcda170e2e6ee98b16a9ba9a6d0dc5b968a8aa7d54a869e9eaeb570f29a9eadbb6bf5bea9b1bb2af0fea7c3b92aacef99a2ca3a9496feb2b44aadb689abcc7fb8abb7d1ef7e91aa8588d8509cbdae99f86b95b5b6aaeb7af1aebd8bd85389a98fbadc7caabcbdaedc73acbab782b54ab69e9b91b54aaee7bb89f7599789fa97ae499a89c0b4d742adb0e1afb15ca3899a85e450a5afffd6c846f6ade18dc17cb2ef898dd33498ea81bab84ebce7998dcc37e2a3; YD00230621733232%3AWM_TID=LQvI0kJrl%2FBBEVUFEBYrZnABLtOqekL9; GDS_PORTAL_NSRSBH=9137MA1Y798Y17; cookie=17737058
那么该如何获取JSESSIONID这个属性的cookie值呢?
1.我们看其路径时在 /shenbao 下,而第一图对应的url是项目根目录,所以我们可以考虑切换到/shenbao目录下在来通过js获取其cookie值
那么如何切换目录呢?
1.其实国税局网页都是通过iframe来页面套页面的,所以我们需要找到对应的iframe(即iframe标签的src属性中包含/shenbao)
那么如何切换iframe呢:
1.可以通过 document.getElementsByTagName(“iframe”)
2.查看返回信息,确定那个是目标iframe,然后在获取该cookie
contentWindow:获取对应的iframe的上下文关系(否则无document对象)
以上同js获取指定目录下的cookie值,如何用python来实现呢(基于pyppeteer框架)
# 该代码有待优化,应该可以通过写js方法直接获取到全部的cookie值,而不是写两遍# 注:该处不可以用page.evaluateOnNewDocument该方法是在新的上下文中执行对应的js方法,所以只会返回Noneco = await page.evaluate("""function get_cookie(){return document.getElementsByTagName("iframe")[0].contentWindow.document.cookie}""")print("cookie3:{}".format(co))co2 = await page.evaluate("""function get_cookie(){return document.getElementsByTagName("iframe")[1].contentWindow.document.cookie}""")print("cookie4:{}".format(co2))