{"id":1008570,"date":"2024-12-27T11:03:55","date_gmt":"2024-12-27T03:03:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008570.html"},"modified":"2024-12-27T11:03:57","modified_gmt":"2024-12-27T03:03:57","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python-%e7%88%ac%e6%8c%89%e9%92%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008570.html","title":{"rendered":"\u5982\u4f55\u7528python \u722c\u6309\u94ae"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083825\/b6571ce2-6f6b-4be1-aa70-d278862b8388.webp\" alt=\"\u5982\u4f55\u7528python \u722c\u6309\u94ae\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u722c\u53d6\u6309\u94ae\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u7f51\u7edc\u722c\u866b\u5e93\u5982Selenium\u3001BeautifulSoup\u3001Scrapy\u7b49\u3002Selenium\u9002\u7528\u4e8e\u52a8\u6001\u7f51\u9875\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u64cd\u4f5c\u3001\u89e3\u6790JavaScript\u3002BeautifulSoup\u4e0eRequests\u7ed3\u5408\u4f7f\u7528\uff0c\u9002\u7528\u4e8e\u9759\u6001\u7f51\u9875\uff0c\u89e3\u6790\u901f\u5ea6\u5feb\u4e14\u7b80\u5355\u3002Scrapy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6293\u53d6\u3002<\/strong>\u9009\u62e9\u9002\u5408\u7684\u5de5\u5177\u540e\uff0c\u6839\u636e\u7f51\u9875\u7ed3\u6784\u5b9a\u4f4d\u6309\u94ae\uff0c\u4f7f\u7528Selenium\u53ef\u6a21\u62df\u70b9\u51fb\uff0cBeautifulSoup\u53ef\u89e3\u6790HTML\u7ed3\u6784\uff0cScrapy\u53ef\u81ea\u52a8\u5316\u722c\u53d6\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u7528\u8fd9\u4e09\u79cd\u5de5\u5177\u6765\u5b9e\u73b0\u6309\u94ae\u722c\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528SELENIUM\u722c\u53d6\u6309\u94ae<\/p>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316\u6d4b\u8bd5Web\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u64cd\u4f5c\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u5305\u542bJavaScript\u7684\u52a8\u6001\u9875\u9762\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u8bbe\u7f6eSelenium<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Selenium\u5e93\u548c\u6d4f\u89c8\u5668\u9a71\u52a8\uff08\u5982ChromeDriver\uff09\u3002\u4f7f\u7528pip\u53ef\u4ee5\u8f7b\u677e\u5b89\u88c5Selenium\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u8f7d\u5bf9\u5e94\u6d4f\u89c8\u5668\u7684\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5e76\u786e\u4fdd\u5b83\u5728\u4f60\u7684\u7cfb\u7edf\u8def\u5f84\u4e2d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Selenium\u5b9a\u4f4d\u548c\u70b9\u51fb\u6309\u94ae<\/li>\n<\/ol>\n<p><p>Selenium\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9a\u4f4d\u7f51\u9875\u5143\u7d20\uff0c\u5982\u901a\u8fc7ID\u3001\u7c7b\u540d\u3001\u6807\u7b7e\u540d\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Selenium\u5b9a\u4f4d\u5e76\u70b9\u51fb\u4e00\u4e2a\u6309\u94ae\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u76ee\u6807\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<h2><strong>\u5b9a\u4f4d\u6309\u94ae\uff08\u5047\u8bbe\u6309\u94ae\u6709\u4e00\u4e2aID\uff09<\/strong><\/h2>\n<p>button = driver.find_element_by_id(&#39;button_id&#39;)<\/p>\n<h2><strong>\u70b9\u51fb\u6309\u94ae<\/strong><\/h2>\n<p>button.click()<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u5904\u7406\u52a8\u6001\u5185\u5bb9<\/li>\n<\/ol>\n<p><p>Selenium\u4e0d\u4ec5\u53ef\u4ee5\u70b9\u51fb\u6309\u94ae\uff0c\u8fd8\u53ef\u4ee5\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u6216\u8005\u52a8\u6001\u5185\u5bb9\u51fa\u73b0\uff0c\u8fd9\u901a\u5e38\u901a\u8fc7WebDriverW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.support.ui import WebDriverWait<\/p>\n<p>from selenium.webdriver.support import expected_conditions as EC<\/p>\n<h2><strong>\u7b49\u5f85\u6309\u94ae\u52a0\u8f7d\u5b8c\u6210<\/strong><\/h2>\n<p>button = WebDriverWait(driver, 10).until(<\/p>\n<p>    EC.presence_of_element_located((By.ID, &#39;button_id&#39;))<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528BEAUTIFULSOUP\u548cREQUESTS\u722c\u53d6\u6309\u94ae<\/p>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684Python\u5e93\uff0c\u901a\u5e38\u4e0eRequests\u5e93\u642d\u914d\u4f7f\u7528\uff0c\u9002\u5408\u5904\u7406\u9759\u6001\u7f51\u9875\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5BeautifulSoup\u548cRequests<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7pip\u5b89\u88c5\u8fd9\u4e24\u4e2a\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Requests\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/li>\n<\/ol>\n<p><p>Requests\u5e93\u7528\u4e8e\u53d1\u51faHTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875HTML\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u53d1\u51fa\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;)<\/p>\n<h2><strong>\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528BeautifulSoup\u89e3\u6790\u6309\u94ae<\/li>\n<\/ol>\n<p><p>BeautifulSoup\u53ef\u4ee5\u901a\u8fc7\u6807\u7b7e\u540d\u3001\u7c7b\u540d\u3001ID\u7b49\u5b9a\u4f4d\u5143\u7d20\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6309\u94ae\u662f\u4e00\u4e2a&lt;a&gt;\u6807\u7b7e<\/p>\n<p>button = soup.find(&#39;a&#39;, {&#39;id&#39;: &#39;button_id&#39;})<\/p>\n<h2><strong>\u83b7\u53d6\u6309\u94ae\u7684\u94fe\u63a5\u6216\u5176\u4ed6\u5c5e\u6027<\/strong><\/h2>\n<p>button_link = button[&#39;href&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7531\u4e8eBeautifulSoup\u65e0\u6cd5\u6267\u884cJavaScript\uff0c\u56e0\u6b64\u65e0\u6cd5\u76f4\u63a5\u201c\u70b9\u51fb\u201d\u6309\u94ae\uff0c\u4f46\u4f60\u53ef\u4ee5\u83b7\u53d6\u6309\u94ae\u7684\u94fe\u63a5\u6216\u5176\u4ed6\u5c5e\u6027\uff0c\u7136\u540e\u53d1\u51fa\u65b0\u7684\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528SCRAPY\u722c\u53d6\u6309\u94ae<\/p>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u6d41\u884c\u7684Web\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u5927\u89c4\u6a21\u7684\u6570\u636e\u6293\u53d6\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Scrapy<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5Scrapy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scrapy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efaScrapy\u9879\u76ee<\/li>\n<\/ol>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Scrapy\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scrapy startproject myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u7f16\u5199\u722c\u866b<\/li>\n<\/ol>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u7684spiders\u6587\u4ef6\u5939\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u722c\u866b\u6587\u4ef6\uff0c\u4f8b\u5982<code>my_spider.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scrapy<\/p>\n<p>class MySpider(scrapy.Spider):<\/p>\n<p>    name = &#39;myspider&#39;<\/p>\n<p>    start_urls = [&#39;http:\/\/example.com&#39;]<\/p>\n<p>    def parse(self, response):<\/p>\n<p>        # \u5047\u8bbe\u6309\u94ae\u662f\u4e00\u4e2a&lt;a&gt;\u6807\u7b7e<\/p>\n<p>        for button in response.css(&#39;a#button_id&#39;):<\/p>\n<p>            yield {<\/p>\n<p>                &#39;link&#39;: button.css(&#39;::attr(href)&#39;).get()<\/p>\n<p>            }<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u8fd0\u884c\u722c\u866b<\/li>\n<\/ol>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u722c\u866b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scrapy crawl myspider<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Scrapy\u4f1a\u81ea\u52a8\u5904\u7406HTTP\u8bf7\u6c42\u5e76\u7ba1\u7406\u6293\u53d6\u7684\u6570\u636e\uff0c\u975e\u5e38\u9002\u5408\u5927\u89c4\u6a21\u7684\u722c\u53d6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u5de5\u5177\uff0c\u722c\u53d6\u6570\u636e\u65f6\u90fd\u5e94\u9075\u5faa\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u4e2d\u7684\u89c4\u5b9a\uff0c\u5e76\u907f\u514d\u5bf9\u670d\u52a1\u5668\u9020\u6210\u8fc7\u5927\u7684\u8d1f\u62c5\u3002\u4f7f\u7528Selenium\u65f6\uff0c\u6ce8\u610f\u6d4f\u89c8\u5668\u7684\u6027\u80fd\u548c\u8d44\u6e90\u6d88\u8017\uff1b\u4f7f\u7528BeautifulSoup\u65f6\uff0c\u6ce8\u610f\u76ee\u6807\u7f51\u9875\u662f\u5426\u4e3a\u52a8\u6001\u52a0\u8f7d\uff1b\u4f7f\u7528Scrapy\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u5176\u5f3a\u5927\u7684\u4e2d\u95f4\u4ef6\u548c\u7ba1\u9053\u673a\u5236\u6765\u5904\u7406\u548c\u5b58\u50a8\u6293\u53d6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fPython\u722c\u866b\uff0c\u4e3a\u4ec0\u4e48\u6211\u9700\u8981\u4f7f\u7528\u5b83\u6765\u722c\u53d6\u6309\u94ae\uff1f<\/strong><br \/>Python\u722c\u866b\u662f\u4e00\u79cd\u81ea\u52a8\u5316\u7a0b\u5e8f\uff0c\u80fd\u591f\u4ece\u7f51\u9875\u4e0a\u63d0\u53d6\u6570\u636e\u3002\u722c\u53d6\u6309\u94ae\u7684\u539f\u56e0\u901a\u5e38\u662f\u4e3a\u4e86\u83b7\u53d6\u4e0e\u6309\u94ae\u76f8\u5173\u8054\u7684\u94fe\u63a5\u6216\u4fe1\u606f\uff0c\u6bd4\u5982\u63d0\u4ea4\u8868\u5355\u3001\u4e0b\u8f7d\u6587\u4ef6\u7b49\u3002\u901a\u8fc7\u4f7f\u7528Python\u7684\u722c\u866b\u6280\u672f\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u6536\u96c6\uff0c\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\uff0c\u5982\u5e02\u573a\u8c03\u7814\u3001\u6570\u636e\u5206\u6790\u7b49\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Python\u722c\u53d6\u6309\u94ae\u65f6\uff0c\u5e94\u8be5\u6ce8\u610f\u54ea\u4e9b\u6280\u672f\u7ec6\u8282\uff1f<\/strong><br \/>\u5728\u722c\u53d6\u6309\u94ae\u65f6\uff0c\u9700\u8981\u5173\u6ce8\u7f51\u9875\u7684\u7ed3\u6784\u548c\u6309\u94ae\u7684HTML\u6807\u7b7e\u3002\u4f7f\u7528\u50cfBeautifulSoup\u6216Scrapy\u8fd9\u6837\u7684\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u89e3\u6790HTML\u6587\u6863\uff0c\u627e\u5230\u76ee\u6807\u6309\u94ae\u3002\u786e\u4fdd\u80fd\u591f\u5904\u7406JavaScript\u751f\u6210\u7684\u5185\u5bb9\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\u3002\u6b64\u5916\uff0c\u9075\u5faa\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\uff0c\u5c0a\u91cd\u7f51\u7ad9\u7684\u722c\u53d6\u89c4\u5219\u4e5f\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u722c\u53d6\u5230\u7684\u6309\u94ae\u6570\u636e\uff1f<\/strong><br \/>\u83b7\u53d6\u6309\u94ae\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u6216\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\uff0c\u65b9\u4fbf\u540e\u7eed\u5206\u6790\u3002\u53ef\u4ee5\u7ed3\u5408Pandas\u5e93\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u5904\u7406\uff0c\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u53ef\u7528\u6027\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u53ef\u89c6\u5316\u5de5\u5177\u5bf9\u6570\u636e\u8fdb\u884c\u56fe\u8868\u5c55\u793a\uff0c\u80fd\u591f\u5e2e\u52a9\u66f4\u597d\u5730\u7406\u89e3\u548c\u5206\u6790\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u722c\u53d6\u6309\u94ae\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u7f51\u7edc\u722c\u866b\u5e93\u5982Selenium\u3001BeautifulSoup\u3001Scrapy\u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1008574,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008570"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1008570"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008570\/revisions"}],"predecessor-version":[{"id":1008576,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008570\/revisions\/1008576"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008574"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}