{"id":1165788,"date":"2025-01-15T15:24:41","date_gmt":"2025-01-15T07:24:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165788.html"},"modified":"2025-01-15T15:24:44","modified_gmt":"2025-01-15T07:24:44","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bc%96%e5%86%99%e7%bd%91%e7%bb%9c%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1165788.html","title":{"rendered":"\u5982\u4f55\u7528Python\u7f16\u5199\u7f51\u7edc\u722c\u866b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25210038\/fad7e25f-55e6-4274-9769-075830a9ba67.webp\" alt=\"\u5982\u4f55\u7528Python\u7f16\u5199\u7f51\u7edc\u722c\u866b\" \/><\/p>\n<p><p> \u7528Python\u7f16\u5199\u7f51\u7edc\u722c\u866b\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u5b9a\u4e49\u76ee\u6807\u3001\u53d1\u8d77\u8bf7\u6c42\u3001\u89e3\u6790\u54cd\u5e94\u3001\u63d0\u53d6\u6570\u636e\u3001\u5b58\u50a8\u6570\u636e\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u5904\u7406\u5f02\u5e38\u3001\u8bbe\u7f6e\u5ef6\u65f6\u3001\u9075\u5b88\u7f51\u7ad9\u7684robots.txt\u534f\u8bae\u3001\u907f\u514dIP\u5c01\u7981<\/strong>\u662f\u7f16\u5199\u9ad8\u6548\u7a33\u5b9a\u7684\u722c\u866b\u7684\u5173\u952e\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u7f51\u7edc\u722c\u866b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93<\/h3>\n<\/p>\n<p><p>Python\u6709\u8bb8\u591a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7f16\u5199\u7f51\u7edc\u722c\u866b\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f<strong>requests<\/strong>\u548c<strong>BeautifulSoup<\/strong>\u3002<strong>requests<\/strong>\u5e93\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u800c<strong>BeautifulSoup<\/strong>\u5e93\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h4>1. Requests\u5e93<\/h4>\n<\/p>\n<p><p>Requests\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u53d1\u9001GET\u548cPOST\u8bf7\u6c42\u3002\u5b83\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5c31\u80fd\u5b8c\u6210\u4e00\u4e2a\u57fa\u672c\u7684HTTP\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&#39;https:\/\/example.com&#39;)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u53d1\u9001\u4e86\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u5e76\u8f93\u51fa\u4e86\u54cd\u5e94\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>2. BeautifulSoup\u5e93<\/h4>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e93\u3002\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6HTML\u6587\u6863\u4e2d\u7684\u6570\u636e\u3002<\/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>&lt;\/body&gt;&lt;\/html&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>soup = BeautifulSoup(html_doc, &#39;html.parser&#39;)<\/p>\n<p>print(soup.prettify())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u89e3\u6790\u4e86\u4e00\u4e2aHTML\u6587\u6863\uff0c\u5e76\u8f93\u51fa\u4e86\u683c\u5f0f\u5316\u540e\u7684HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u53d1\u8d77\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u722c\u866b\u65f6\uff0c\u9996\u5148\u9700\u8981\u53d1\u8d77HTTP\u8bf7\u6c42\u4ee5\u83b7\u53d6\u76ee\u6807\u7f51\u9875\u7684\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u6765\u53d1\u8d77GET\u6216POST\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    html_content = response.text<\/p>\n<p>else:<\/p>\n<p>    print(f&#39;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to retrieve the page. Status code: {response.status_code}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u53d1\u9001\u4e86\u4e00\u4e2aGET\u8bf7\u6c42\uff0c\u5e76\u68c0\u67e5\u54cd\u5e94\u7684\u72b6\u6001\u7801\u3002\u5982\u679c\u8bf7\u6c42\u6210\u529f\uff0c\u8f93\u51fa\u7f51\u9875\u5185\u5bb9\uff1b\u5426\u5219\uff0c\u8f93\u51fa\u5931\u8d25\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790\u54cd\u5e94<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u540e\uff0c\u9700\u8981\u89e3\u6790HTML\u6587\u6863\u4ee5\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u6765\u89e3\u6790HTML\u6587\u6863\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u6240\u6709\u94fe\u63a5<\/strong><\/h2>\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<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u89e3\u6790\u4e86HTML\u6587\u6863\uff0c\u5e76\u63d0\u53d6\u4e86\u6240\u6709\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u63d0\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u89e3\u6790HTML\u6587\u6863\u540e\uff0c\u53ef\u4ee5\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002BeautifulSoup\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u63d0\u53d6HTML\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u627e\u6240\u6709\u5e26\u6709class\u5c5e\u6027\u4e3asister\u7684&lt;a&gt;\u6807\u7b7e<\/p>\n<p>sisters = soup.find_all(&#39;a&#39;, class_=&#39;sister&#39;)<\/p>\n<p>for sister in sisters:<\/p>\n<p>    print(sister.text, sister.get(&#39;href&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u67e5\u627e\u4e86\u6240\u6709\u5e26\u6709class\u5c5e\u6027\u4e3asister\u7684<a>\u6807\u7b7e\uff0c\u5e76\u8f93\u51fa\u4e86\u5b83\u4eec\u7684\u6587\u672c\u5185\u5bb9\u548c\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b58\u50a8\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u63d0\u53d6\u5230\u7684\u6570\u636e\u53ef\u4ee5\u5b58\u50a8\u5230\u6570\u636e\u5e93\u3001\u6587\u4ef6\u6216\u5176\u4ed6\u5b58\u50a8\u4ecb\u8d28\u4e2d\u3002\u8fd9\u91cc\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230CSV\u6587\u4ef6\u4e3a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [{&#39;name&#39;: sister.text, &#39;link&#39;: sister.get(&#39;href&#39;)} for sister in sisters]<\/p>\n<p>with open(&#39;sisters.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.DictWriter(file, fieldnames=[&#39;name&#39;, &#39;link&#39;])<\/p>\n<p>    writer.writeheader()<\/p>\n<p>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u5c06\u63d0\u53d6\u5230\u7684\u6570\u636e\u5b58\u50a8\u5230\u4e86CSV\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u722c\u866b\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u7f51\u7edc\u9519\u8bef\u3001\u89e3\u6790\u9519\u8bef\u7b49\u3002\u9700\u8981\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u4ee5\u4fdd\u8bc1\u722c\u866b\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from requests.exceptions import RequestException<\/p>\n<p>try:<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    response.raise_for_status()<\/p>\n<p>except RequestException as e:<\/p>\n<p>    print(f&#39;Error: {e}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u5904\u7406\u4e86\u8bf7\u6c42\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8bbe\u7f6e\u5ef6\u65f6<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u5bf9\u76ee\u6807\u7f51\u7ad9\u9020\u6210\u8fc7\u5927\u538b\u529b\uff0c\u53ef\u4ee5\u5728\u6bcf\u6b21\u8bf7\u6c42\u4e4b\u95f4\u8bbe\u7f6e\u5ef6\u65f6\u3002\u53ef\u4ee5\u4f7f\u7528time\u5e93\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>time.sleep(2)  # \u5ef6\u65f62\u79d2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u5728\u6bcf\u6b21\u8bf7\u6c42\u4e4b\u95f4\u5ef6\u65f62\u79d2\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u9075\u5b88\u7f51\u7ad9\u7684robots.txt\u534f\u8bae<\/h3>\n<\/p>\n<p><p>robots.txt\u6587\u4ef6\u662f\u7f51\u7ad9\u7528\u6765\u544a\u8bc9\u722c\u866b\u54ea\u4e9b\u9875\u9762\u53ef\u4ee5\u6293\u53d6\uff0c\u54ea\u4e9b\u9875\u9762\u4e0d\u80fd\u6293\u53d6\u7684\u534f\u8bae\u3002\u5728\u7f16\u5199\u722c\u866b\u65f6\uff0c\u9700\u8981\u9075\u5b88\u76ee\u6807\u7f51\u7ad9\u7684robots.txt\u534f\u8bae\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from urllib.robotparser import RobotFileParser<\/p>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>robots_url = url + &#39;\/robots.txt&#39;<\/p>\n<p>rp = RobotFileParser()<\/p>\n<p>rp.set_url(robots_url)<\/p>\n<p>rp.read()<\/p>\n<p>if rp.can_fetch(&#39;*&#39;, url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    # \u7ee7\u7eed\u5904\u7406\u54cd\u5e94<\/p>\n<p>else:<\/p>\n<p>    print(&#39;Crawling disallowed by robots.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u68c0\u67e5\u4e86\u76ee\u6807\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\uff0c\u5e76\u6839\u636erobots.txt\u534f\u8bae\u51b3\u5b9a\u662f\u5426\u6293\u53d6\u9875\u9762\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u907f\u514dIP\u5c01\u7981<\/h3>\n<\/p>\n<p><p>\u5728\u9891\u7e41\u8bbf\u95ee\u540c\u4e00\u4e2a\u7f51\u7ad9\u65f6\uff0c\u53ef\u80fd\u4f1a\u88ab\u7f51\u7ad9\u5c01\u7981IP\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4ee3\u7406\u3001\u8bbe\u7f6e\u8bf7\u6c42\u5934\u7b49\u65b9\u5f0f\u6765\u907f\u514dIP\u5c01\u7981\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7requests\u5e93\u7684proxies\u53c2\u6570\u6765\u4f7f\u7528\u4ee3\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">proxies = {<\/p>\n<p>    &#39;http&#39;: &#39;http:\/\/10.10.1.10:3128&#39;,<\/p>\n<p>    &#39;https&#39;: &#39;http:\/\/10.10.1.10:1080&#39;,<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, proxies=proxies)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528\u4e86HTTP\u548cHTTPS\u4ee3\u7406\u3002<\/p>\n<\/p>\n<p><h4>2. \u8bbe\u7f6e\u8bf7\u6c42\u5934<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7requests\u5e93\u7684headers\u53c2\u6570\u6765\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff0c\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u8bf7\u6c42\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\/91.0.4472.124 Safari\/537.36&#39;,<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u8bbe\u7f6e\u4e86User-Agent\u8bf7\u6c42\u5934\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5b8c\u6574\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7f51\u7edc\u722c\u866b\uff0c\u722c\u53d6\u8c46\u74e3\u7535\u5f71Top250\uff0c\u5e76\u5c06\u6570\u636e\u5b58\u50a8\u5230CSV\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import csv<\/p>\n<p>import time<\/p>\n<p>def fetch_page(url):<\/p>\n<p>    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\/91.0.4472.124 Safari\/537.36&#39;,<\/p>\n<p>    }<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url, headers=headers)<\/p>\n<p>        response.raise_for_status()<\/p>\n<p>        return response.text<\/p>\n<p>    except requests.RequestException as e:<\/p>\n<p>        print(f&#39;Error: {e}&#39;)<\/p>\n<p>        return None<\/p>\n<p>def parse_page(html):<\/p>\n<p>    soup = BeautifulSoup(html, &#39;html.parser&#39;)<\/p>\n<p>    movies = []<\/p>\n<p>    for item in soup.find_all(&#39;div&#39;, class_=&#39;item&#39;):<\/p>\n<p>        rank = item.find(&#39;em&#39;).text<\/p>\n<p>        title = item.find(&#39;span&#39;, class_=&#39;title&#39;).text<\/p>\n<p>        rating = item.find(&#39;span&#39;, class_=&#39;rating_num&#39;).text<\/p>\n<p>        movies.append({&#39;rank&#39;: rank, &#39;title&#39;: title, &#39;rating&#39;: rating})<\/p>\n<p>    return movies<\/p>\n<p>def save_to_csv(data, filename):<\/p>\n<p>    with open(filename, mode=&#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        writer = csv.DictWriter(file, fieldnames=[&#39;rank&#39;, &#39;title&#39;, &#39;rating&#39;])<\/p>\n<p>        writer.writeheader()<\/p>\n<p>        writer.writerows(data)<\/p>\n<p>def main():<\/p>\n<p>    base_url = &#39;https:\/\/movie.douban.com\/top250&#39;<\/p>\n<p>    all_movies = []<\/p>\n<p>    for start in range(0, 250, 25):<\/p>\n<p>        url = f&#39;{base_url}?start={start}&#39;<\/p>\n<p>        html = fetch_page(url)<\/p>\n<p>        if html:<\/p>\n<p>            movies = parse_page(html)<\/p>\n<p>            all_movies.extend(movies)<\/p>\n<p>            time.sleep(2)  # \u5ef6\u65f62\u79d2<\/p>\n<p>    save_to_csv(all_movies, &#39;douban_top250.csv&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528requests\u548cBeautifulSoup\u5e93\u7f16\u5199\u4e00\u4e2a\u7f51\u7edc\u722c\u866b\u3002\u722c\u866b\u9996\u5148\u53d1\u8d77HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u89e3\u6790HTML\u6587\u6863\u63d0\u53d6\u6570\u636e\uff0c\u6700\u540e\u5c06\u6570\u636e\u5b58\u50a8\u5230CSV\u6587\u4ef6\u4e2d\u3002\u540c\u65f6\uff0c\u722c\u866b\u8fd8\u5904\u7406\u4e86\u8bf7\u6c42\u5f02\u5e38\uff0c\u5e76\u5728\u6bcf\u6b21\u8bf7\u6c42\u4e4b\u95f4\u8bbe\u7f6e\u4e86\u5ef6\u65f6\u4ee5\u907f\u514d\u5bf9\u76ee\u6807\u7f51\u7ad9\u9020\u6210\u8fc7\u5927\u538b\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u8fdb\u9636\u6280\u5de7<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528Scrapy\u6846\u67b6<\/h4>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Python\u722c\u866b\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u7f16\u5199\u590d\u6742\u7684\u722c\u866b\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u81ea\u52a8\u5904\u7406\u8bf7\u6c42\u3001\u89e3\u6790\u54cd\u5e94\u3001\u5b58\u50a8\u6570\u636e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scrapy<\/p>\n<p>class QuotesSpider(scrapy.Spider):<\/p>\n<p>    name = &#39;quotes&#39;<\/p>\n<p>    start_urls = [<\/p>\n<p>        &#39;http:\/\/quotes.toscrape.com\/page\/1\/&#39;,<\/p>\n<p>    ]<\/p>\n<p>    def parse(self, response):<\/p>\n<p>        for quote in response.css(&#39;div.quote&#39;):<\/p>\n<p>            yield {<\/p>\n<p>                &#39;text&#39;: quote.css(&#39;span.text::text&#39;).get(),<\/p>\n<p>                &#39;author&#39;: quote.css(&#39;span.small::text&#39;).get(),<\/p>\n<p>                &#39;tags&#39;: quote.css(&#39;div.tags a.tag::text&#39;).getall(),<\/p>\n<p>            }<\/p>\n<p>        next_page = response.css(&#39;li.next a::attr(href)&#39;).get()<\/p>\n<p>        if next_page is not None:<\/p>\n<p>            yield response.follow(next_page, self.parse)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528Scrapy\u6846\u67b6\u7f16\u5199\u4e86\u4e00\u4e2a\u722c\u866b\uff0c\u4ece<a href=\"http:\/\/quotes.toscrape.com\/%E9%A1%B5%E9%9D%A2%E7%88%AC%E5%8F%96%E5%90%8D%E8%A8%80%E5%B9%B6%E6%8F%90%E5%8F%96%E7%9B%B8%E5%85%B3%E6%95%B0%E6%8D%AE%E3%80%82\">http:\/\/quotes.toscrape.com\/\u9875\u9762\u722c\u53d6\u540d\u8a00\u5e76\u63d0\u53d6\u76f8\u5173\u6570\u636e\u3002<\/a><\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65IO<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u9ad8\u6548\u7684\u722c\u866b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65IO\u6765\u63d0\u9ad8\u722c\u53d6\u901f\u5ea6\u3002Python\u7684threading\u548casyncio\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u591a\u7ebf\u7a0b\u548c\u5f02\u6b65IO\u3002<\/p>\n<\/p>\n<p><h5>\u591a\u7ebf\u7a0b\u793a\u4f8b<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def fetch_page(url):<\/p>\n<p>    # \u53d1\u8d77\u8bf7\u6c42\u5e76\u5904\u7406\u54cd\u5e94<\/p>\n<p>    pass<\/p>\n<p>urls = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;, ...]<\/p>\n<p>threads = []<\/p>\n<p>for url in urls:<\/p>\n<p>    thread = threading.Thread(target=fetch_page, args=(url,))<\/p>\n<p>    thread.start()<\/p>\n<p>    threads.append(thread)<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u5f02\u6b65IO\u793a\u4f8b<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">import aiohttp<\/p>\n<p>import asyncio<\/p>\n<p>async def fetch_page(session, url):<\/p>\n<p>    async with session.get(url) as response:<\/p>\n<p>        return await response.text()<\/p>\n<p>async def main():<\/p>\n<p>    async with aiohttp.ClientSession() as session:<\/p>\n<p>        tasks = []<\/p>\n<p>        urls = [&#39;https:\/\/example.com\/page1&#39;, &#39;https:\/\/example.com\/page2&#39;, ...]<\/p>\n<p>        for url in urls:<\/p>\n<p>            tasks.append(fetch_page(session, url))<\/p>\n<p>        responses = await asyncio.gather(*tasks)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u529f\u80fd\u5b8c\u5584\u7684Python\u7f51\u7edc\u722c\u866b\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u5904\u7406\u5f02\u5e38\u3001\u8bbe\u7f6e\u5ef6\u65f6\u3001\u9075\u5b88\u7f51\u7ad9\u7684robots.txt\u534f\u8bae\u3001\u907f\u514dIP\u5c01\u7981<\/strong>\u662f\u7f16\u5199\u9ad8\u6548\u7a33\u5b9a\u7684\u722c\u866b\u7684\u5173\u952e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u76ee\u6807\u7f51\u7ad9\u7684\u7279\u70b9\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u6280\u672f\u548c\u6846\u67b6\u6765\u7f16\u5199\u722c\u866b\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684requests\u548cBeautifulSoup\u7ec4\u5408\uff0c\u8fd8\u662f\u529f\u80fd\u5f3a\u5927\u7684Scrapy\u6846\u67b6\uff0c\u90fd\u80fd\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5b8c\u6210\u722c\u53d6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u5b66\u4e60Python\u7f51\u7edc\u722c\u866b\u7684\u57fa\u7840\u77e5\u8bc6\uff1f<\/strong><br \/>\u8981\u5b66\u4e60Python\u7f51\u7edc\u722c\u866b\uff0c\u5efa\u8bae\u4ece\u4e86\u89e3HTTP\u534f\u8bae\u548c\u7f51\u9875\u7ed3\u6784\uff08\u5982HTML\u548cCSS\uff09\u5f00\u59cb\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u7ebf\u8bfe\u7a0b\u3001\u4e66\u7c4d\u6216\u6559\u7a0b\u6765\u638c\u63e1Python\u7684\u57fa\u672c\u8bed\u6cd5\uff0c\u968f\u540e\u5b66\u4e60\u4f7f\u7528\u50cfRequests\u548cBeautiful Soup\u8fd9\u6837\u7684\u5e93\u6765\u53d1\u9001\u8bf7\u6c42\u548c\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002\u52a0\u5165\u4e00\u4e9b\u5b9e\u9645\u9879\u76ee\uff0c\u7ec3\u4e60\u6293\u53d6\u4e0d\u540c\u7c7b\u578b\u7684\u7f51\u7ad9\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u722c\u866b\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n<p><strong>\u5728\u7f16\u5199\u7f51\u7edc\u722c\u866b\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f51\u7ad9\u7684\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u8bb8\u591a\u7f51\u7ad9\u90fd\u6709\u53cd\u722c\u866b\u673a\u5236\u6765\u9632\u6b62\u81ea\u52a8\u6293\u53d6\u6570\u636e\u3002\u5e94\u5bf9\u8fd9\u4e9b\u673a\u5236\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5408\u7406\u7684\u8bf7\u6c42\u9891\u7387\uff0c\u907f\u514d\u77ed\u65f6\u95f4\u5185\u53d1\u9001\u8fc7\u591a\u8bf7\u6c42\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eUser-Agent\u3001\u4f7f\u7528\u4ee3\u7406IP\u3001\u4f2a\u88c5\u8bf7\u6c42\u5934\u4ee5\u53ca\u8fdb\u884c\u968f\u673a\u65f6\u95f4\u95f4\u9694\u7684\u8bf7\u6c42\u6765\u51cf\u5c11\u88ab\u5c01\u7981\u7684\u98ce\u9669\u3002\u6b64\u5916\uff0c\u5b66\u4e60\u5982\u4f55\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u6a21\u62df\u7528\u6237\u884c\u4e3a\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ed5\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u53cd\u722c\u866b\u63aa\u65bd\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7f16\u5199\u7f51\u7edc\u722c\u866b\u65f6\uff0c\u5982\u4f55\u5b58\u50a8\u6293\u53d6\u7684\u6570\u636e\uff1f<\/strong><br \/>\u6293\u53d6\u5230\u7684\u6570\u636e\u53ef\u4ee5\u5b58\u50a8\u5728\u591a\u79cd\u683c\u5f0f\u4e2d\uff0c\u5305\u62ecCSV\u3001JSON\u6216\u6570\u636e\u5e93\uff08\u5982SQLite\u548cMongoDB\uff09\u3002\u9009\u62e9\u5b58\u50a8\u683c\u5f0f\u65f6\uff0c\u8003\u8651\u6570\u636e\u7684\u7ed3\u6784\u548c\u540e\u7eed\u4f7f\u7528\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u636e\uff0cCSV\u6587\u4ef6\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u800c\u5bf9\u4e8e\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0cJSON\u66f4\u4e3a\u5408\u9002\u3002\u5982\u679c\u9700\u8981\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u67e5\u8be2\u5927\u91cf\u6570\u636e\uff0c\u6570\u636e\u5e93\u5219\u662f\u66f4\u7406\u60f3\u7684\u9009\u62e9\u3002\u5229\u7528Python\u7684pandas\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u5b58\u50a8\u548c\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u7f16\u5199\u7f51\u7edc\u722c\u866b\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u5b9a\u4e49\u76ee\u6807\u3001\u53d1\u8d77\u8bf7\u6c42\u3001\u89e3\u6790\u54cd\u5e94\u3001\u63d0\u53d6\u6570\u636e\u3001\u5b58\u50a8\u6570\u636e\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1165794,"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\/1165788"}],"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=1165788"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165788\/revisions"}],"predecessor-version":[{"id":1165799,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165788\/revisions\/1165799"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1165794"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1165788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1165788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1165788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}