{"id":1008019,"date":"2024-12-27T10:59:00","date_gmt":"2024-12-27T02:59:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008019.html"},"modified":"2024-12-27T10:59:17","modified_gmt":"2024-12-27T02:59:17","slug":"python%e5%a6%82%e4%bd%95%e9%87%87%e9%9b%86%e7%bd%91%e9%a1%b5%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1008019.html","title":{"rendered":"Python\u5982\u4f55\u91c7\u96c6\u7f51\u9875\u4fe1\u606f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083501\/77e80fa5-5a67-4204-bb08-76338596b2b5.webp\" alt=\"Python\u5982\u4f55\u91c7\u96c6\u7f51\u9875\u4fe1\u606f\" \/><\/p>\n<p><p> <strong>Python\u91c7\u96c6\u7f51\u9875\u4fe1\u606f\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3001\u5229\u7528BeautifulSoup\u5e93\u89e3\u6790HTML\u3001\u901a\u8fc7Selenium\u8fdb\u884c\u52a8\u6001\u7f51\u9875\u6570\u636e\u7684\u6293\u53d6\u3001\u4f7f\u7528Scrapy\u6846\u67b6\u8fdb\u884c\u5927\u89c4\u6a21\u6570\u636e\u91c7\u96c6\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528requests\u548cBeautifulSoup\u662f\u6700\u57fa\u7840\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u9759\u6001\u9875\u9762\u7684\u6570\u636e\u91c7\u96c6\uff1b\u800cSelenium\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406JavaScript\u6e32\u67d3\u7684\u52a8\u6001\u7f51\u9875\u3002Scrapy\u5219\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6846\u67b6\uff0c\u9002\u5408\u4e8e\u5bf9\u5927\u578b\u7f51\u7ad9\u8fdb\u884c\u9ad8\u6548\u7684\u6570\u636e\u6293\u53d6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528REQUESTS\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/p>\n<\/p>\n<p><p>Requests\u5e93\u662fPython\u4e2d\u6700\u6d41\u884c\u7684HTTP\u5e93\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u65b9\u4fbf\u5730\u8fdb\u884cHTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u7684HTML\u6e90\u4ee3\u7801\u3002\u4f7f\u7528requests\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u6a21\u62df\u6d4f\u89c8\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u4f7f\u7528Requests\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5requests\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165requests\u5e93\uff0c\u5e76\u4f7f\u7528\u5176\u529f\u80fd\u6765\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(response.text)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to retrieve the webpage. Status code: {response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165requests\u5e93\uff0c\u7136\u540e\u5b9a\u4e49\u8981\u8bbf\u95ee\u7684URL\uff0c\u63a5\u7740\u4f7f\u7528<code>requests.get()<\/code>\u51fd\u6570\u53d1\u9001GET\u8bf7\u6c42\uff0c\u6700\u540e\u68c0\u67e5\u54cd\u5e94\u7684\u72b6\u6001\u7801\uff0c\u5e76\u6253\u5370\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5904\u7406\u8bf7\u6c42\u5934\u548cCookies<\/strong><\/li>\n<\/ol>\n<p><p>\u6709\u65f6\u5019\uff0c\u4e3a\u4e86\u6a21\u62df\u66f4\u771f\u5b9e\u7684\u6d4f\u89c8\u5668\u8bf7\u6c42\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff08headers\uff09\u548cCookies\u3002requests\u5e93\u5141\u8bb8\u6211\u4eec\u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934\u548cCookies\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">headers = {<\/p>\n<p>    &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/58.0.3029.110 Safari\/537.3&#39;<\/p>\n<p>}<\/p>\n<p>cookies = {<\/p>\n<p>    &#39;session&#39;: &#39;abc123&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers, cookies=cookies)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u5934\u548cCookies\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed5\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u53cd\u722c\u866b\u63aa\u65bd\uff0c\u63d0\u9ad8\u722c\u866b\u7684\u6210\u529f\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5229\u7528BEAUTIFULSOUP\u5e93\u89e3\u6790HTML<\/p>\n<\/p>\n<p><p>\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u540e\uff0c\u6211\u4eec\u9700\u8981\u89e3\u6790HTML\u6765\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002BeautifulSoup\u662f\u4e00\u4e2a\u6d41\u884c\u7684Python\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u65b9\u4fbf\u5730\u89e3\u6790HTML\u548cXML\u6587\u6863\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5BeautifulSoup\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>BeautifulSoup\u5e93\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u89e3\u6790HTML\u6587\u6863<\/strong><\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u6587\u6863\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_doc = &quot;&quot;&quot;<\/p>\n<p>&lt;html&gt;&lt;head&gt;&lt;title&gt;The Dormouse&#39;s story&lt;\/title&gt;&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Dormouse&#39;s story&lt;\/b&gt;&lt;\/p&gt;<\/p>\n<p>&lt;p class=&quot;story&quot;&gt;Once upon a time there were three little sisters; and their names were<\/p>\n<p>&lt;a href=&quot;http:\/\/example.com\/elsie&quot; class=&quot;sister&quot; id=&quot;link1&quot;&gt;Elsie&lt;\/a&gt;,<\/p>\n<p>&lt;a href=&quot;http:\/\/example.com\/lacie&quot; class=&quot;sister&quot; id=&quot;link2&quot;&gt;Lacie&lt;\/a&gt; and<\/p>\n<p>&lt;a href=&quot;http:\/\/example.com\/tillie&quot; class=&quot;sister&quot; id=&quot;link3&quot;&gt;Tillie&lt;\/a&gt;;<\/p>\n<p>and they lived at the bottom of a well.&lt;\/p&gt;<\/p>\n<p>&lt;p class=&quot;story&quot;&gt;...&lt;\/p&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>soup = BeautifulSoup(html_doc, &#39;html.parser&#39;)<\/p>\n<p>print(soup.title)<\/p>\n<p>print(soup.title.name)<\/p>\n<p>print(soup.title.string)<\/p>\n<p>print(soup.find_all(&#39;a&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165BeautifulSoup\u5e93\uff0c\u7136\u540e\u5c06HTML\u6587\u6863\u89e3\u6790\u4e3a\u4e00\u4e2aBeautifulSoup\u5bf9\u8c61\u3002\u63a5\u7740\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u63d0\u4f9b\u7684\u5404\u79cd\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u63d0\u53d6HTML\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u63d0\u53d6\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><p>BeautifulSoup\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u6765\u67e5\u627e\u548c\u63d0\u53d6\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>find()<\/code>\u3001<code>find_all()<\/code>\u3001<code>select()<\/code>\u7b49\u65b9\u6cd5\u6765\u6839\u636e\u6807\u7b7e\u3001\u5c5e\u6027\u3001CSS\u9009\u62e9\u5668\u7b49\u67e5\u627eHTML\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u6240\u6709\u94fe\u63a5<\/p>\n<p>links = soup.find_all(&#39;a&#39;)<\/p>\n<p>for link in links:<\/p>\n<p>    print(link.get(&#39;href&#39;))<\/p>\n<h2><strong>\u63d0\u53d6\u5e26\u6709\u7279\u5b9a\u7c7b\u7684\u6bb5\u843d<\/strong><\/h2>\n<p>paragraphs = soup.find_all(&#39;p&#39;, class_=&#39;story&#39;)<\/p>\n<p>for paragraph in paragraphs:<\/p>\n<p>    print(paragraph.get_text())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u4eceHTML\u6587\u6863\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u901a\u8fc7SELENIUM\u8fdb\u884c\u52a8\u6001\u7f51\u9875\u6570\u636e\u6293\u53d6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u9700\u8981JavaScript\u6e32\u67d3\u7684\u52a8\u6001\u7f51\u9875\uff0crequests\u548cBeautifulSoup\u53ef\u80fd\u65e0\u6cd5\u83b7\u5f97\u5b8c\u6574\u7684\u7f51\u9875\u5185\u5bb9\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u6765\u6a21\u62df\u771f\u5b9e\u7684\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u83b7\u53d6\u52a8\u6001\u7f51\u9875\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Selenium\u5e93\u548cWebDriver<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Selenium\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4e0b\u8f7d\u5e76\u5b89\u88c5\u76f8\u5e94\u7684WebDriver\uff0c\u4f8b\u5982ChromeDriver\uff0c\u7528\u4e8e\u9a71\u52a8\u6d4f\u89c8\u5668\u3002\u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u9700\u8981\u5c06\u5176\u8def\u5f84\u52a0\u5165\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528Selenium\u6293\u53d6\u52a8\u6001\u7f51\u9875<\/strong><\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Selenium\u6293\u53d6\u52a8\u6001\u7f51\u9875\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u521b\u5efa\u6d4f\u89c8\u5668\u5bf9\u8c61<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/strong><\/h2>\n<p>driver.implicitly_wait(10)<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>content = driver.page_source<\/p>\n<h2><strong>\u6253\u5370\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>print(content)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2aChrome\u6d4f\u89c8\u5668\u5bf9\u8c61\uff0c\u4f7f\u7528<code>get()<\/code>\u65b9\u6cd5\u6253\u5f00\u7f51\u9875\uff0c\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u540e\uff0c\u901a\u8fc7<code>page_source<\/code>\u5c5e\u6027\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u6700\u540e\u5173\u95ed\u6d4f\u89c8\u5668\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5904\u7406\u52a8\u6001\u5185\u5bb9<\/strong><\/li>\n<\/ol>\n<p><p>Selenium\u53ef\u4ee5\u901a\u8fc7\u6267\u884cJavaScript\u548c\u7b49\u5f85\u9875\u9762\u5143\u7d20\u52a0\u8f7d\u7b49\u65b9\u5f0f\u5904\u7406\u52a8\u6001\u5185\u5bb9\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>execute_script()<\/code>\u65b9\u6cd5\u6267\u884cJavaScript\u4ee3\u7801\uff0c\u4ee5\u53ca<code>WebDriverWait<\/code>\u7c7b\u7b49\u5f85\u7279\u5b9a\u7684\u9875\u9762\u5143\u7d20\u52a0\u8f7d\u3002<\/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\u7279\u5b9a\u5143\u7d20\u52a0\u8f7d<\/strong><\/h2>\n<p>element = WebDriverWait(driver, 10).until(<\/p>\n<p>    EC.presence_of_element_located((By.ID, &#39;myDynamicElement&#39;))<\/p>\n<p>)<\/p>\n<h2><strong>\u83b7\u53d6\u5143\u7d20\u5185\u5bb9<\/strong><\/h2>\n<p>print(element.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5904\u7406\u52a8\u6001\u7f51\u9875\u4e2d\u7684\u6570\u636e\u6293\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528SCRAPY\u6846\u67b6\u8fdb\u884c\u5927\u89c4\u6a21\u6570\u636e\u91c7\u96c6<\/p>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u6846\u67b6\uff0c\u4e13\u4e3a\u5927\u89c4\u6a21\u6570\u636e\u6293\u53d6\u548c\u722c\u866b\u5f00\u53d1\u800c\u8bbe\u8ba1\u3002\u5b83\u652f\u6301\u5f02\u6b65\u5904\u7406\u3001\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u5b58\u50a8\u7b49\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u4e8e\u5bf9\u5927\u578b\u7f51\u7ad9\u8fdb\u884c\u9ad8\u6548\u7684\u6570\u636e\u6293\u53d6\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Scrapy\u6846\u67b6<\/strong><\/li>\n<\/ol>\n<p><p>Scrapy\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\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><strong>\u521b\u5efaScrapy\u9879\u76ee<\/strong><\/li>\n<\/ol>\n<p><p>Scrapy\u63d0\u4f9b\u4e86\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u521b\u5efa\u548c\u7ba1\u7406\u722c\u866b\u9879\u76ee\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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><strong>\u7f16\u5199\u722c\u866b<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Scrapy\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u722c\u866b\u7c7b\uff0c\u5b9a\u4e49\u8981\u6293\u53d6\u7684\u9875\u9762\u548c\u63d0\u53d6\u7684\u6570\u636e\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Scrapy\u722c\u866b\u793a\u4f8b\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>        for href in response.css(&#39;a::attr(href)&#39;).getall():<\/p>\n<p>            yield response.follow(href, self.parse)<\/p>\n<p>        yield {<\/p>\n<p>            &#39;title&#39;: response.css(&#39;title::text&#39;).get(),<\/p>\n<p>            &#39;url&#39;: response.url,<\/p>\n<p>        }<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u722c\u866b\u7c7b<code>MySpider<\/code>\uff0c\u6307\u5b9a\u4e86\u8d77\u59cbURL\uff0c\u5e76\u5728<code>parse()<\/code>\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u4e86\u63d0\u53d6\u6570\u636e\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u8fd0\u884c\u722c\u866b<\/strong><\/li>\n<\/ol>\n<p><p>\u5b8c\u6210\u722c\u866b\u4ee3\u7801\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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\u7406\u8bf7\u6c42\u3001\u89e3\u6790\u54cd\u5e94\uff0c\u5e76\u5c06\u63d0\u53d6\u7684\u6570\u636e\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u6216\u5b58\u50a8\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6570\u636e\u5b58\u50a8\u4e0e\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u6570\u636e\u6293\u53d6\u540e\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5b58\u50a8\u548c\u5904\u7406\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u5b58\u50a8\u65b9\u5f0f\uff0c\u5305\u62ec\u672c\u5730\u6587\u4ef6\u5b58\u50a8\u3001\u6570\u636e\u5e93\u5b58\u50a8\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b58\u50a8\u5230\u672c\u5730\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u6293\u53d6\u7684\u6570\u636e\u5b58\u50a8\u5230CSV\u3001JSON\u3001Excel\u7b49\u683c\u5f0f\u7684\u672c\u5730\u6587\u4ef6\u4e2d\u3002Python\u5185\u7f6e\u7684<code>csv<\/code>\u548c<code>json<\/code>\u6a21\u5757\uff0c\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93<code>pandas<\/code>\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [<\/p>\n<p>    {&#39;title&#39;: &#39;Example&#39;, &#39;url&#39;: &#39;http:\/\/example.com&#39;},<\/p>\n<p>    # \u66f4\u591a\u6570\u636e<\/p>\n<p>]<\/p>\n<h2><strong>\u5b58\u50a8\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.csv&#39;, &#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as csvfile:<\/p>\n<p>    fieldnames = [&#39;title&#39;, &#39;url&#39;]<\/p>\n<p>    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)<\/p>\n<p>    writer.writeheader()<\/p>\n<p>    for item in data:<\/p>\n<p>        writer.writerow(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5b58\u50a8\u5230\u6570\u636e\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u5c06\u5176\u5b58\u50a8\u5230\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982MySQL\u3001PostgreSQL\uff09\u6216NoSQL\u6570\u636e\u5e93\uff08\u5982MongoDB\uff09\u4e2d\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u5982<code>pymysql<\/code>\u3001<code>psycopg2<\/code>\u3001<code>pymongo<\/code>\u7b49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u6570\u636e\u7684\u5b58\u50a8\u548c\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<h2><strong>\u8fde\u63a5\u5230\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;user&#39;,<\/p>\n<p>    password=&#39;passwd&#39;,<\/p>\n<p>    db=&#39;database&#39;,<\/p>\n<p>    charset=&#39;utf8mb4&#39;,<\/p>\n<p>    cursorclass=pymysql.cursors.DictCursor<\/p>\n<p>)<\/p>\n<p>try:<\/p>\n<p>    with connection.cursor() as cursor:<\/p>\n<p>        # \u521b\u5efa\u8868<\/p>\n<p>        sql = &quot;CREATE TABLE IF NOT EXISTS `webdata` (`id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255), `url` varchar(255), PRIMARY KEY (`id`))&quot;<\/p>\n<p>        cursor.execute(sql)<\/p>\n<p>        # \u63d2\u5165\u6570\u636e<\/p>\n<p>        sql = &quot;INSERT INTO `webdata` (`title`, `url`) VALUES (%s, %s)&quot;<\/p>\n<p>        cursor.execute(sql, (&#39;Example&#39;, &#39;http:\/\/example.com&#39;))<\/p>\n<p>    connection.commit()<\/p>\n<p>finally:<\/p>\n<p>    connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u7684\u4ecb\u7ecd\u548c\u793a\u4f8b\uff0c\u76f8\u4fe1\u60a8\u5df2\u7ecf\u5bf9Python\u5982\u4f55\u91c7\u96c6\u7f51\u9875\u4fe1\u606f\u6709\u4e86\u4e00\u4e2a\u5168\u9762\u7684\u4e86\u89e3\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u9759\u6001\u7f51\u9875\uff0c\u8fd8\u662f\u590d\u6742\u7684\u52a8\u6001\u7f51\u9875\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u9ad8\u6548\u3001\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u6765\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u6570\u636e\u91c7\u96c6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u8fdb\u884c\u7f51\u9875\u4fe1\u606f\u91c7\u96c6\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5f3a\u5927\u7684\u5e93\u6765\u8fdb\u884c\u7f51\u9875\u4fe1\u606f\u91c7\u96c6\uff0c\u4f8b\u5982Beautiful Soup\u3001Requests\u548cScrapy\u3002Requests\u5e93\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u800cBeautiful Soup\u5219\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790HTML\u548cXML\u6587\u6863\uff0c\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002Scrapy\u662f\u4e00\u4e2a\u5168\u9762\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u8fdb\u884c\u590d\u6742\u7684\u7f51\u9875\u91c7\u96c6\u4efb\u52a1\u3002\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u6216\u6846\u67b6\u6765\u5b9e\u73b0\u4fe1\u606f\u91c7\u96c6\u3002<\/p>\n<p><strong>\u5728\u91c7\u96c6\u7f51\u9875\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u548c\u9053\u5fb7\u95ee\u9898\uff1f<\/strong><br \/>\u7f51\u9875\u4fe1\u606f\u91c7\u96c6\u9700\u8981\u9075\u5faa\u4e00\u5b9a\u7684\u6cd5\u5f8b\u548c\u9053\u5fb7\u89c4\u8303\u3002\u7528\u6237\u5e94\u786e\u4fdd\u9075\u5faa\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u89c4\u5b9a\uff0c\u907f\u514d\u8fc7\u4e8e\u9891\u7e41\u7684\u8bf7\u6c42\u5bfc\u81f4\u670d\u52a1\u5668\u8d1f\u62c5\u8fc7\u91cd\u3002\u6b64\u5916\uff0c\u91c7\u96c6\u7684\u5185\u5bb9\u5e94\u907f\u514d\u4fb5\u72af\u7248\u6743\uff0c\u6700\u597d\u5728\u4f7f\u7528\u91c7\u96c6\u6570\u636e\u65f6\u6ce8\u660e\u6765\u6e90\u6216\u83b7\u53d6\u6388\u6743\u3002\u4e86\u89e3\u5e76\u5c0a\u91cd\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\u5bf9\u4e8e\u5408\u6cd5\u548c\u9053\u5fb7\u7684\u4fe1\u606f\u91c7\u96c6\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u52a8\u6001\u7f51\u9875\u7684\u5185\u5bb9\u91c7\u96c6\uff1f<\/strong><br \/>\u52a8\u6001\u7f51\u9875\u901a\u5e38\u4f7f\u7528JavaScript\u751f\u6210\u5185\u5bb9\uff0c\u4f20\u7edf\u7684HTML\u89e3\u6790\u65b9\u6cd5\u53ef\u80fd\u65e0\u6cd5\u83b7\u53d6\u6240\u9700\u4fe1\u606f\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u7b49\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u52a0\u8f7dJavaScript\u751f\u6210\u7684\u5185\u5bb9\u3002\u901a\u8fc7Selenium\uff0c\u7528\u6237\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\u6d4b\u8bd5\u548c\u4fe1\u606f\u91c7\u96c6\uff0c\u8bbf\u95ee\u52a8\u6001\u52a0\u8f7d\u7684\u6570\u636e\u3002\u6b64\u5916\uff0c\u4f7f\u7528API\uff08\u5982\u679c\u7f51\u7ad9\u63d0\u4f9b\uff09\u4e5f\u662f\u83b7\u53d6\u52a8\u6001\u5185\u5bb9\u7684\u6709\u6548\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u91c7\u96c6\u7f51\u9875\u4fe1\u606f\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3001\u5229\u7528BeautifulSoup\u5e93\u89e3\u6790 [&hellip;]","protected":false},"author":3,"featured_media":1008054,"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\/1008019"}],"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=1008019"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008019\/revisions"}],"predecessor-version":[{"id":1008059,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008019\/revisions\/1008059"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008054"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}