{"id":1064,"date":"2018-07-26T22:10:30","date_gmt":"2018-07-26T14:10:30","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=1064"},"modified":"2018-07-26T22:10:30","modified_gmt":"2018-07-26T14:10:30","slug":"%e7%ac%ac%e5%8d%81%e8%8a%82%ef%bc%9a%e5%8a%a8%e6%80%81%e6%b8%b2%e6%9f%93%e9%a1%b5%e9%9d%a2%e7%88%ac%e5%8f%96","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/carwler\/1064\/","title":{"rendered":"\u7b2c\u5341\u8282\uff1a\u52a8\u6001\u6e32\u67d3\u9875\u9762\u722c\u53d6"},"content":{"rendered":"<p>\u672c\u9875\u4ee3\u7801\u53ef\u4ee5\u5728<a href=\"https:\/\/github.com\/Sniper970119\/Spider\/tree\/master\/20180721\" target=\"_blank\" rel=\"noopener\" data-slimstat=\"5\">\u8fd9\u91cc<\/a>\u4e0b\u8f7d\u3002<br \/>\n\u5f53\u7f51\u9875\u4e0d\u662f\u4f7f\u7528Ajax\u52a8\u6001\u6e32\u67d3\u7684\u65f6\u5019\uff0c\u5c31\u6ca1\u529e\u6cd5\u4f7f\u7528Ajax\uff0c\u540c\u65f6\u8fd8\u6709\u4e00\u4e9b\u4f7f\u7528Ajax\u7684\uff0c\u4f46\u662f\u4f7f\u7528\u7684\u662f\u52a0\u5bc6\u63a5\u53e3\uff0c\u6240\u4ee5\u5f88\u96be\u627e\u51fa\u89c4\u5f8b\u722c\u53d6\uff0c\u6240\u4ee5\u6211\u4eec\u4f7f\u7528\u6a21\u62df\u6d4f\u89c8\u5668\u6765\u8fdb\u884c\u722c\u53d6\uff0c\u505a\u5230\u53ef\u89c6\u5373\u53ef\u722c\u3002<\/p>\n<h3>1.Selenium\u7684\u4f7f\u7528<\/h3>\n<p>selenium\u662f\u4e00\u4e2a\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u5177\uff0c\u5229\u7528\u5b83\u53ef\u4ee5\u9a71\u52a8\u6d4f\u89c8\u5668\u6267\u884c\u7279\u5b9a\u7684\u52a8\u4f5c\uff0c\u5982\u70b9\u51fb\u3001\u4e0b\u62c9\u7b49\u64cd\u4f5c\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u83b7\u53d6\u6d4f\u89c8\u5668\u5f53\u524d\u5448\u73b0\u7684\u9875\u9762\u7684\u6e90\u4ee3\u7801\uff0c\u505a\u5230\u53ef\u89c6\u5373\u53ef\u722c\u3002<br \/>\n<a href=\"http:\/\/selenium-python-zh.readthedocs.io\/en\/latest\/\" target=\"_blank\" rel=\"noopener\">selenium\u4e2d\u6587\u6587\u6863<\/a><br \/>\n\u58f0\u660e\u6d4f\u89c8\u5668\u5bf9\u8c61\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">browser = webdriver.Chrome()<\/pre>\n<p>\u8bbf\u95ee\u9875\u9762\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">browser.get('https:\/\/www.baidu.com')\nprint(browser.page_source)\nbrowser.close()\n<\/pre>\n<p>\u67e5\u627e\u8282\u70b9&amp;\u8282\u70b9\u4ea4\u4e92\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2018\/07\/75.png\" alt=\"\" class=\"alignnone size-full wp-image-1065\" width=\"1570\" height=\"799\" \/><br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u641c\u7d22\u6846\u7684id\u662fq\uff0cname\u4e5f\u662fq\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528find_element_by_name( )\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528find_element_by_id( )\u6765\u83b7\u53d6\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u6839\u636eXPath\u6216\u8005CSS\u9009\u62e9\u5668\u7b49\u5728\u64cd\u4f5c\u3002<br \/>\nbrowser.execute_script(&#8216;window.scrollTo(0, document.body.scrollHeight)&#8217;)<br \/>\nbrowser.execute_script(&#8216;alert(&#8220;To Bottom&#8221;)&#8217;)<br \/>\n\u76f4\u63a5\u62d6\u5230\u5e95\u90e8\u5e76\u663e\u793a\u7a97\u53e3 To Bottom<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># \u67e5\u627e\u5355\u4e2a\u8282\u70b9\n# \u4f7f\u7528 find_element_by_name\ninput_first = browser.find_element_by_name('q')\n# \u4f7f\u7528 XPath\n# input_second = browser.find_element_by_xpath('\/\/*[@id=\"q\"]')\n# \u4f7f\u7528 CSS\u9009\u62e9\u5668\n# input_third = browser.find_element_by_css_selector('#q')\ninput_first.send_keys('iPad')\ntime.sleep(1)\ninput_first.clear()\ninput_first.send_keys('iPhone')\nbutton = browser.find_element_by_class_name('btn-search')\nbutton.click()\nbrowser.execute_script('window.scrollTo(0, document.body.scrollHeight)')\nbrowser.execute_script('alert(\"To Bottom\")')\n<\/pre>\n<p>\u7ed3\u679c\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2018\/07\/Video_2018-07-26_213622_20180726213824.gif\" alt=\"\" class=\"alignnone size-full wp-image-1067\" width=\"400\" height=\"439\" \/><br \/>\n\u52a8\u4f5c\u94fe\uff1a<br \/>\nActionChain(browser)<br \/>\n.drag_and_drop(element1, element2)<br \/>\n.perform( )<\/p>\n<h5>\u83b7\u53d6\u5c5e\u6027\uff1a<\/h5>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528get_attribute( key)\u83b7\u53d6\u8282\u70b9\u5c5e\u6027\uff0c\u524d\u63d0\u662f\u9009\u4e2d\u8fd9\u4e2a\u8282\u70b9\u3002<br \/>\n\u6211\u4eec\u53ef\u4ee5\u8c03\u7528.text \u6765\u83b7\u53d6\u6587\u672c\uff0c\u548cBeautiful Soup\u7684get_text( )\u548cpyquery\u7684text\uff08\uff09\u4e00\u6837\u3002<br \/>\n\u8fd8\u53ef\u4ee5\u4f7f\u7528id\u6765\u83b7\u53d6id\uff0clocation\u6765\u83b7\u53d6\u76f8\u5bf9\u4f4d\u7f6e\uff0ctag_name\u6765\u83b7\u53d6\u6807\u7b7e\u540d\u79f0\uff0csize\u6765\u83b7\u53d6\u8282\u70b9\u5927\u5c0f\u7b49\u3002<\/p>\n<h5>\u5207\u6362Frame\uff1a<\/h5>\n<p>\u5f53\u9700\u8981\u5207\u6362\u5230\u5b50frame\u65f6\uff0c\u4f7f\u7528switch_to.frame(name),\u4f7f\u7528switch_to.parent_frame()\u5207\u6362\u5230\u7236frame\u3002<\/p>\n<h5>\u5ef6\u65f6\u7b49\u5f85\uff1a<\/h5>\n<p>\u6709\u65f6\u5019\u9700\u8981\u5ef6\u65f6\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u624d\u80fd\u628a\u6e90\u7801\u5b8c\u5168\u7684\u52a0\u8f7d\u8fdb\u6765\u3002<br \/>\n\u9690\u5f0f\u7b49\u5f85\uff1a\u7b49\u5f85\u4e00\u6bb5\u56fa\u5b9a\u7684\u65f6\u95f4\uff0c\u5982\u679c\u8282\u70b9\u4ecd\u6ca1\u6709\u627e\u5230\u629b\u51fa\u5f02\u5e38\u3002<br \/>\n\u663e\u793a\u7b49\u5f85\uff1a\u89c4\u5b9a\u4e00\u4e2a\u7b49\u5f85\u65f6\u95f4\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">wait = WebDriverWait(browser, 10)\ninput = wait.until(EC.presence_of_all_elements_located((By.ID, 'q')))\nbutton = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search')))<\/pre>\n<p>\u8fd9\u91cc\u9996\u5148\u5f15\u5165WebDriverWait\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u6307\u5b9a\u6700\u957f\u7b49\u5f85\u65f6\u95f4\uff0c\u7136\u540e\u8c03\u7528\u5b83\u7684until\uff08\uff09\u65b9\u6cd5\uff0c\u4f20\u5165\u8981\u7b49\u5f85\u7684\u6761\u4ef6\u3002<\/p>\n<h5>\u524d\u8fdb\u548c\u540e\u9000<\/h5>\n<p>browser.back()<br \/>\nbrowser.forward()<\/p>\n<h5>Cookies<\/h5>\n<p>browser.get_cookies() \u83b7\u53d6cookies<br \/>\nbrowser.add_cookies() \u589e\u52a0cookies<br \/>\nbrowser.delete_all_cookies() \u5220\u9664\u6240\u6709cookies<\/p>\n<h5>\u9009\u9879\u5361\u7ba1\u7406\uff1a<\/h5>\n<p>\u65b0\u5efa\u9009\u9879\u5361\u6253\u5f00\u6dd8\u5b9d\uff0c\u5207\u6362\u56de\u6765\u6253\u5f00python\u5b98\u7f51\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># \u9009\u9879\u5361\u7ba1\u7406\nbrowser = webdriver.Chrome()\nbrowser.get('https:\/\/www.baidu.com')\nbrowser.execute_script('window.open()')\nprint(browser.window_handles)\nbrowser.switch_to_window(browser.window_handles[1])\nbrowser.get('https:\/\/www.taobao.com')\ntime.sleep(1)\nbrowser.switch_to_window(browser.window_handles[0])\nbrowser.get('https:\/\/python.org')\n<\/pre>\n<h5>\u5f02\u5e38\u5904\u7406\uff1a<\/h5>\n<p>\u5982\u679c\u6ca1\u6709\u627e\u5230\u8282\u70b9\uff0c\u629b\u51fa\u5f02\u5e38NoSuchElementException<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u9875\u4ee3\u7801\u53ef\u4ee5\u5728\u8fd9\u91cc\u4e0b\u8f7d\u3002 \u5f53\u7f51\u9875\u4e0d\u662f\u4f7f\u7528Ajax\u52a8\u6001\u6e32\u67d3\u7684\u65f6\u5019\uff0c\u5c31\u6ca1\u529e\u6cd5\u4f7f\u7528Ajax\uff0c\u540c\u65f6\u8fd8\u6709\u4e00\u4e9b\u4f7f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12],"tags":[],"views":3594,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/1064"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1064"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/1064\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1064"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}