{"id":153245,"date":"2019-08-21T10:39:19","date_gmt":"2019-08-21T02:39:19","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=153245"},"modified":"2019-08-09T16:40:14","modified_gmt":"2019-08-09T08:40:14","slug":"selenium-cookies","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/selenium-cookies.html","title":{"rendered":"selenium\u64cd\u4f5ccookies\u5b9e\u73b0\u514d\u5bc6\u767b\u5f55"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4e00\u76f4\u60f3\u7528selenium\u5b9e\u73b0\u4e2a\u5c0f\u529f\u80fd\uff0c\u6bd4\u5982\u53d1\u5fae\u535a\u4e4b\u7c7b\u7684\uff0c\u4f46\u662f\u6709\u7684\u7f51\u7ad9\u5728\u767b\u5f55\u4f1a\u6709\u9a8c\u8bc1\u7801\uff0c\u6ca1\u60f3\u5230\u592a\u597d\u7684\u65b9\u6cd5\u89e3\u51b3\uff0c\u4e8e\u662f\u60f3\u5230\u5229\u7528cookies\u6765\u767b\u5f55\u7f51\u7ad9\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u83b7\u53d6\u4e00\u4e2a\u53ef\u7528\u7684cookies<\/strong><\/span><\/div>\n

\u83b7\u53d6\u7684cookies\u4f1a\u4fdd\u5b58\u4e3a\u4e00\u4e2ajson\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u4e0d\u8fc7\u5728\u6253\u5f00\u7f51\u7ad9\u540e\uff0c\u767b\u5f55\u64cd\u4f5c\u9700\u8981\u81ea\u5df1\u624b\u52a8\u8fdb\u884c\u4e00\u4e0b\uff0c\u4ee3\u7801\u4e2d\u6709\u6ce8\u91ca\uff0c\u5927\u5bb6\u53ef\u4ee5\u6ce8\u610f\u4e00\u4e0b\uff0c<\/p>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\n#!\/usr\/bin\/python\r\n#coding=utf-8\r\n     \r\n from selenium import webdriver\r\n import time\r\n import json\r\n     \r\n def get_cookies(test_url):\r\n     #\u4fdd\u5b58cookies\u7684\u6587\u4ef6\r\n     file = 'cookies.json'\r\n     #\u6253\u5f00\u9700\u8981\u83b7\u53d6cookies\u7684\u7f51\u7ad9\r\n     driver = webdriver.Firefox()\r\n     driver.implicitly_wait(5)\r\n     driver.get(test_url)\r\n     driver.maximize_window()\r\n     #\u7f51\u7ad9\u6253\u5f00\u540e\uff0c\u5728\u65f6\u95f4\u5185\u624b\u52a8\u6267\u884c\u767b\u5f55\u64cd\u4f5c\r\n     time.sleep(60)\r\n     #\u767b\u5f55\u6210\u529f\u540e\uff0c\u83b7\u53d6cookies\u5e76\u4fdd\u5b58\u4e3ajson\u683c\u5f0f\r\n     cookies = driver.get_cookies()\r\n     fp = open(file,'w')\r\n     json.dump(cookies,fp)\r\n     fp.close()\r\n     #\u5173\u95ed\u6d4f\u89c8\u5668\r\n     driver.close()\r\n     \r\n if __name__ == \"__main__\":\r\n     url = raw_input('please input url:')\r\n     get_cookies(url)\r\n<\/pre>\n
\u5229\u7528cookies\u767b\u5f55\u7f51\u7ad9<\/strong><\/span><\/div>\n

\u5148\u6253\u5f00\u8981\u767b\u5f55\u7684\u7f51\u7ad9\uff0c\u7136\u540e\u5c06cookies\u4ece\u4e4b\u524d\u4fdd\u5b58\u7684json\u6587\u4ef6\u8bfb\u53d6\u51fa\u6765\uff0c\u7136\u540e\u6dfb\u52a0\u5230\u6d4f\u89c8\u5668\uff0c\u5237\u65b0\u754c\u9762\uff0c\u5373\u53ef\u5b9e\u73b0\u767b\u5f55\uff0c\u7136\u540e\u4fbf\u53ef\u4ee5\u8fdb\u884c\u53d1\u5fae\u535a\u7684\u64cd\u4f5c\uff0c\u5177\u4f53\u5143\u7d20\u5b9a\u4f4d\uff0c\u8bf7\u5927\u5bb6\u767e\u5ea6selenium\u7684\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\uff0c\u6b64\u5904\u4e0d\u518d\u8d58\u8ff0\u3002\u4ee3\u7801\u4e2d\u4e5f\u7ed9\u51fa\u4e86\u4e0d\u6253\u5f00\u6d4f\u89c8\u5668\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u529f\u80fd\u3002<\/p>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\n    #!\/usr\/bin\/python\r\n    #coding=utf-8\r\n     \r\n    from selenium import webdriver\r\n    import json\r\n    import time\r\n     \r\n    file = 'cookies.json'\r\n    #\u6253\u5f00\u6d4f\u89c8\u5668\uff0c\u82e5\u4e0d\u60f3\u6253\u5f00\u6d4f\u89c8\u5668\u8fd0\u884c\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u6ce8\u91ca\u6389\u7684\u65b9\u6cd5\r\n    driver = webdriver.Firefox()\r\n     \r\n    #\u4ee5headless\u6a21\u5f0f\u8fd0\u884c\u811a\u672c\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u6253\u5f00\u6d4f\u89c8\u5668\u5bf9\u7f51\u7ad9\u8fdb\u884c\u64cd\u4f5c\r\n    #option = webdriver.FirefoxOptions()\r\n    #option.add_argument(argument='-headless')\r\n    #driver = webdriver.Firefox(firefox_options=option)\r\n     \r\n    #\u6253\u5f00\u7f51\u5740\r\n    def open_url(url):\r\n        driver.get(url)\r\n        driver.implicitly_wait(5)\r\n        driver.maximize_window()\r\n     \r\n    #\u5411\u6d4f\u89c8\u5668\u6dfb\u52a0\u4fdd\u5b58\u7684cookies\r\n    def add_cookies():\r\n        fp = open(file,'r')\r\n        cookies = json.load(fp)\r\n        fp.close()\r\n        for cookie in cookies:\r\n            driver.add_cookie(cookie)  \r\n     \r\n    if __name__ == \"__main__\":\r\n        #\u8f93\u5165\u8981\u6253\u5f00\u7684\u7f51\u7ad9\u5e76\u6253\u5f00\r\n        test_url = raw_input('please input url:')\r\n        open_url(test_url)\r\n        time.sleep(5)\r\n        #\u6dfb\u52a0cookies\u540e\u5237\u65b0\u754c\u9762\uff0c\u5b9e\u73b0\u514d\u5bc6\u767b\u5f55\r\n        add_cookies()\r\n        driver.refresh()\r\n        # \u53d1\u9001\u5fae\u535a\r\n        #\u5b9a\u4f4d\u5230\u5fae\u535a\u8f93\u5165\u6846\uff0c\u70b9\u51fb\u8f93\u5165\u6846\r\n        driver.find_element_by_xpath(\"\/\/textarea[@class='W_input']\").click()\r\n        time.sleep(2)\r\n        #\u8f93\u5165\u9700\u8981\u53d1\u9001\u7684\u5185\u5bb9\r\n        driver.find_element_by_xpath(\"\/\/textarea[@class='W_input']\").send_keys('Test message')\r\n        time.sleep(3)\r\n        #\u70b9\u51fb\u53d1\u9001\u6309\u94ae\r\n        driver.find_element\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u83b7\u53d6\u7684cookies\u4f1a\u4fdd\u5b58\u4e3a\u4e00\u4e2ajson\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u4e0d\u8fc7\u5728\u6253\u5f00\u7f51\u7ad9\u540e\uff0c\u767b\u5f55\u64cd\u4f5c\u9700\u8981\u81ea\u5df1\u624b\u52a8\u8fdb\u884c\u4e00\u4e0b\uff0c\u4ee3\u7801\u4e2d\u6709\u6ce8 […]<\/p>\n","protected":false},"author":1479,"featured_media":153246,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-153245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/153245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=153245"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/153245\/revisions"}],"predecessor-version":[{"id":153351,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/153245\/revisions\/153351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/153246"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=153245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=153245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=153245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}