{"id":1133203,"date":"2025-01-08T21:04:17","date_gmt":"2025-01-08T13:04:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133203.html"},"modified":"2025-01-08T21:04:20","modified_gmt":"2025-01-08T13:04:20","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%8e%b7%e5%8f%96%e7%bd%91%e4%b8%8a%e7%9a%84%e5%85%a8%e9%83%a8%e7%94%b5%e8%a7%86%e5%89%a7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133203.html","title":{"rendered":"\u5982\u4f55\u7528python\u83b7\u53d6\u7f51\u4e0a\u7684\u5168\u90e8\u7535\u89c6\u5267"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102519\/685e5e0c-5e05-4d12-aa7c-e8ff05d81c76.webp\" alt=\"\u5982\u4f55\u7528python\u83b7\u53d6\u7f51\u4e0a\u7684\u5168\u90e8\u7535\u89c6\u5267\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u83b7\u53d6\u7f51\u4e0a\u7684\u5168\u90e8\u7535\u89c6\u5267<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u7528Python\u83b7\u53d6\u7f51\u4e0a\u7684\u5168\u90e8\u7535\u89c6\u5267\uff0c\u53ef\u4ee5\u901a\u8fc7<strong>\u7f51\u7edc\u722c\u866b\u3001API\u8c03\u7528\u3001\u6570\u636e\u89e3\u6790<\/strong>\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u7f51\u7edc\u722c\u866b\u662f\u4e00\u79cd\u901a\u8fc7\u7a0b\u5e8f\u81ea\u52a8\u8bbf\u95ee\u7f51\u7ad9\u5e76\u63d0\u53d6\u6570\u636e\u7684\u6280\u672f\u3002API\u8c03\u7528\u5219\u662f\u5229\u7528\u7f51\u7ad9\u63d0\u4f9b\u7684\u5b98\u65b9\u63a5\u53e3\u83b7\u53d6\u6570\u636e\u3002\u6570\u636e\u89e3\u6790\u662f\u5c06\u83b7\u53d6\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u6574\u7406\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u901a\u8fc7\u7f51\u7edc\u722c\u866b\u6765\u83b7\u53d6\u7535\u89c6\u5267\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7f51\u7edc\u722c\u866b\u7684\u57fa\u672c\u6982\u5ff5\u548c\u539f\u7406<\/h3>\n<\/p>\n<p><p>\u7f51\u7edc\u722c\u866b\u662f\u4e00\u79cd\u81ea\u52a8\u5316\u7a0b\u5e8f\uff0c\u65e8\u5728\u901a\u8fc7\u6a21\u62df\u4eba\u5de5\u8bbf\u95ee\u7f51\u7ad9\u7684\u884c\u4e3a\uff0c\u6279\u91cf\u6293\u53d6\u7f51\u9875\u5185\u5bb9\u5e76\u63d0\u53d6\u6709\u4ef7\u503c\u7684\u4fe1\u606f\u3002\u5b9e\u73b0\u7f51\u7edc\u722c\u866b\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a<strong>\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790HTML\u9875\u9762\u3001\u63d0\u53d6\u76ee\u6807\u6570\u636e\u3001\u5b58\u50a8\u6570\u636e<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u53d1\u9001HTTP\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u53d1\u9001HTTP\u8bf7\u6c42\u662f\u7f51\u7edc\u722c\u866b\u7684\u7b2c\u4e00\u6b65\uff0c\u901a\u8fc7Python\u7684requests\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002requests\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u53d1\u9001GET\u6216POST\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u670d\u52a1\u5668\u54cd\u5e94\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>html_content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u89e3\u6790HTML\u9875\u9762<\/h4>\n<\/p>\n<p><p>\u89e3\u6790HTML\u9875\u9762\u662f\u5c06\u83b7\u53d6\u7684\u7f51\u9875\u5185\u5bb9\u8f6c\u5316\u4e3a\u53ef\u64cd\u4f5c\u7684\u5bf9\u8c61\uff0c\u901a\u5e38\u4f7f\u7528BeautifulSoup\u5e93\u6765\u5b9e\u73b0\u3002BeautifulSoup\u53ef\u4ee5\u5c06\u590d\u6742\u7684HTML\u6587\u6863\u8f6c\u5316\u4e3a\u6811\u5f62\u7ed3\u6784\uff0c\u65b9\u4fbf\u63d0\u53d6\u7279\u5b9a\u5143\u7d20\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<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u63d0\u53d6\u76ee\u6807\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u63d0\u53d6\u76ee\u6807\u6570\u636e\u662f\u6839\u636e\u7f51\u9875\u7ed3\u6784\u548c\u76ee\u6807\u4fe1\u606f\u7684\u5b9a\u4f4d\uff0c\u4f7f\u7528BeautifulSoup\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u5177\u4f53\u7684\u6570\u636e\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u6709find\u3001find_all\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">titles = soup.find_all(&#39;h2&#39;, class_=&#39;title&#39;)<\/p>\n<p>for title in titles:<\/p>\n<p>    print(title.get_text())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u5b58\u50a8\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5b58\u50a8\u6570\u636e\u662f\u5c06\u63d0\u53d6\u5230\u7684\u4fe1\u606f\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u5206\u6790\u548c\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684csv\u3001json\u6a21\u5757\u6216\u8005\u6570\u636e\u5e93\u5e93\u5982sqlite3\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;tv_shows.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&#39;Title&#39;, &#39;URL&#39;])<\/p>\n<p>    for title in titles:<\/p>\n<p>        writer.writerow([title.get_text(), title.find(&#39;a&#39;)[&#39;href&#39;]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6784\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u7f51\u7edc\u722c\u866b\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u4e86\u89e3\u4e86\u7f51\u7edc\u722c\u866b\u7684\u57fa\u672c\u6982\u5ff5\u548c\u539f\u7406\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u6784\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u7f51\u7edc\u722c\u866b\u7a0b\u5e8f\u3002\u8be5\u7a0b\u5e8f\u5c06\u4ece\u67d0\u4e2a\u7535\u89c6\u5267\u7f51\u7ad9\u6293\u53d6\u6240\u6709\u7535\u89c6\u5267\u7684\u4fe1\u606f\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u672c\u5730\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9009\u62e9\u76ee\u6807\u7f51\u7ad9<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u5305\u542b\u4e30\u5bcc\u7535\u89c6\u5267\u4fe1\u606f\u7684\u7f51\u7ad9\u4f5c\u4e3a\u76ee\u6807\u7f51\u7ad9\u3002\u53ef\u4ee5\u9009\u62e9\u4e00\u4e9b\u77e5\u540d\u7684\u5f71\u89c6\u5267\u7f51\u7ad9\uff0c\u5982\u8c46\u74e3\u7535\u5f71\u3001IMDb\u7b49\u3002\u8fd9\u4e9b\u7f51\u7ad9\u901a\u5e38\u4f1a\u63d0\u4f9b\u8be6\u7ec6\u7684\u7535\u89c6\u5267\u4fe1\u606f\uff0c\u5305\u62ec\u6807\u9898\u3001\u5bfc\u6f14\u3001\u6f14\u5458\u3001\u8bc4\u5206\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5206\u6790\u7f51\u9875\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5176\u6b21\uff0c\u9700\u8981\u5206\u6790\u76ee\u6807\u7f51\u7ad9\u7684\u7f51\u9875\u7ed3\u6784\uff0c\u786e\u5b9a\u9700\u8981\u6293\u53d6\u7684\u5177\u4f53\u4fe1\u606f\u7684\u4f4d\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528\u6d4f\u89c8\u5668\u7684\u5f00\u53d1\u8005\u5de5\u5177\u6765\u67e5\u770b\u7f51\u9875\u7684HTML\u4ee3\u7801\uff0c\u5e76\u627e\u5230\u76ee\u6807\u4fe1\u606f\u6240\u5728\u7684\u6807\u7b7e\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u7f16\u5199\u722c\u866b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u5206\u6790\u7684\u7f51\u9875\u7ed3\u6784\uff0c\u7f16\u5199\u722c\u866b\u4ee3\u7801\u6765\u6293\u53d6\u7535\u89c6\u5267\u4fe1\u606f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u4ece\u67d0\u4e2a\u7535\u89c6\u5267\u7f51\u7ad9\u6293\u53d6\u7535\u89c6\u5267\u7684\u6807\u9898\u548c\u94fe\u63a5\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<h2><strong>\u76ee\u6807\u7f51\u7ad9\u7684URL<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com\/tv_shows&#39;<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94<\/strong><\/h2>\n<p>response = requests.get(url)<\/p>\n<p>html_content = response.text<\/p>\n<h2><strong>\u89e3\u6790HTML\u9875\u9762<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u7535\u89c6\u5267\u4fe1\u606f<\/strong><\/h2>\n<p>tv_shows = soup.find_all(&#39;div&#39;, class_=&#39;tv_show&#39;)<\/p>\n<p>data = []<\/p>\n<p>for show in tv_shows:<\/p>\n<p>    title = show.find(&#39;h2&#39;, class_=&#39;title&#39;).get_text()<\/p>\n<p>    link = show.find(&#39;a&#39;)[&#39;href&#39;]<\/p>\n<p>    data.append([title, link])<\/p>\n<h2><strong>\u5b58\u50a8\u6570\u636e\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;tv_shows.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&#39;Title&#39;, &#39;URL&#39;])<\/p>\n<p>    writer.writerows(data)<\/p>\n<p>print(&#39;Data saved to tv_shows.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u5904\u7406\u7ffb\u9875<\/h4>\n<\/p>\n<p><p>\u5f88\u591a\u7f51\u7ad9\u7684\u7535\u89c6\u5267\u4fe1\u606f\u4f1a\u5206\u9875\u663e\u793a\uff0c\u9700\u8981\u5904\u7406\u7ffb\u9875\u903b\u8f91\u3002\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u7f51\u9875\u7684\u5206\u9875\u673a\u5236\uff0c\u627e\u5230\u4e0b\u4e00\u9875\u7684URL\uff0c\u5e76\u5728\u722c\u866b\u4ee3\u7801\u4e2d\u6dfb\u52a0\u5faa\u73af\u6765\u6293\u53d6\u6240\u6709\u9875\u9762\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5904\u7406\u7ffb\u9875\u903b\u8f91<\/p>\n<p>while True:<\/p>\n<p>    # \u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    html_content = response.text<\/p>\n<p>    # \u89e3\u6790HTML\u9875\u9762<\/p>\n<p>    soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p>    # \u63d0\u53d6\u7535\u89c6\u5267\u4fe1\u606f<\/p>\n<p>    tv_shows = soup.find_all(&#39;div&#39;, class_=&#39;tv_show&#39;)<\/p>\n<p>    for show in tv_shows:<\/p>\n<p>        title = show.find(&#39;h2&#39;, class_=&#39;title&#39;).get_text()<\/p>\n<p>        link = show.find(&#39;a&#39;)[&#39;href&#39;]<\/p>\n<p>        data.append([title, link])<\/p>\n<p>    # \u67e5\u627e\u4e0b\u4e00\u9875\u7684URL<\/p>\n<p>    next_page = soup.find(&#39;a&#39;, class_=&#39;next_page&#39;)<\/p>\n<p>    if next_page:<\/p>\n<p>        url = next_page[&#39;href&#39;]<\/p>\n<p>    else:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u63d0\u9ad8\u722c\u866b\u6027\u80fd\u548c\u7a33\u5b9a\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7f51\u7edc\u722c\u866b\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u95ee\u9898\uff0c\u5982\u7f51\u7ad9\u53cd\u722c\u673a\u5236\u3001\u7f51\u7edc\u5ef6\u8fdf\u7b49\u3002\u4e3a\u4e86\u63d0\u9ad8\u722c\u866b\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u8bf7\u6c42\u5934<\/h4>\n<\/p>\n<p><p>\u8bbe\u7f6e\u8bf7\u6c42\u5934\u53ef\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u7684\u884c\u4e3a\uff0c\u907f\u514d\u88ab\u7f51\u7ad9\u8bc6\u522b\u4e3a\u722c\u866b\u800c\u5c01\u7981\u3002\u53ef\u4ee5\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\u6dfb\u52a0User-Agent\u7b49\u5934\u4fe1\u606f\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><h4>2\u3001\u6dfb\u52a0\u5ef6\u8fdf<\/h4>\n<\/p>\n<p><p>\u6dfb\u52a0\u5ef6\u8fdf\u53ef\u4ee5\u51cf\u8f7b\u670d\u52a1\u5668\u7684\u8d1f\u62c5\uff0c\u907f\u514d\u9891\u7e41\u8bf7\u6c42\u5bfc\u81f4\u88ab\u5c01\u7981\u3002\u53ef\u4ee5\u4f7f\u7528time\u6a21\u5757\u6dfb\u52a0\u968f\u673a\u5ef6\u8fdf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import random<\/p>\n<p>time.sleep(random.uniform(1, 3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee3\u7406\u53ef\u4ee5\u9690\u85cf\u771f\u5b9e\u7684IP\u5730\u5740\uff0c\u907f\u514d\u56e0\u9891\u7e41\u8bf7\u6c42\u88ab\u5c01\u7981\u3002\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u4ee3\u7406\u670d\u52a1\u6216\u514d\u8d39\u4ee3\u7406IP\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">proxies = {<\/p>\n<p>    &#39;http&#39;: &#39;http:\/\/proxy_ip:proxy_port&#39;,<\/p>\n<p>    &#39;https&#39;: &#39;https:\/\/proxy_ip:proxy_port&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, headers=headers, proxies=proxies)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u52a8\u6001\u7f51\u9875<\/h3>\n<\/p>\n<p><p>\u5f88\u591a\u73b0\u4ee3\u7f51\u7ad9\u4f7f\u7528JavaScript\u52a8\u6001\u52a0\u8f7d\u6570\u636e\uff0c\u4f20\u7edf\u7684\u722c\u866b\u65b9\u6cd5\u65e0\u6cd5\u76f4\u63a5\u83b7\u53d6\u8fd9\u4e9b\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Selenium<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Selenium\u5e93\u548c\u5bf9\u5e94\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\uff0c\u5982ChromeDriver\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Selenium\u83b7\u53d6\u52a8\u6001\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Selenium\u53ef\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u8bbf\u95ee\u7f51\u9875\uff0c\u5e76\u6267\u884cJavaScript\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u4f7f\u7528Selenium\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u7535\u89c6\u5267\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.common.by import By<\/p>\n<p>from selenium.webdriver.chrome.service import Service<\/p>\n<p>from selenium.webdriver.common.keys import Keys<\/p>\n<p>import time<\/p>\n<p>import csv<\/p>\n<h2><strong>\u8bbe\u7f6eChromeDriver\u8def\u5f84<\/strong><\/h2>\n<p>service = Service(&#39;path\/to\/chromedriver&#39;)<\/p>\n<p>driver = webdriver.Chrome(service=service)<\/p>\n<h2><strong>\u6253\u5f00\u76ee\u6807\u7f51\u7ad9<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com\/tv_shows&#39;<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/strong><\/h2>\n<p>time.sleep(5)<\/p>\n<h2><strong>\u63d0\u53d6\u7535\u89c6\u5267\u4fe1\u606f<\/strong><\/h2>\n<p>tv_shows = driver.find_elements(By.CLASS_NAME, &#39;tv_show&#39;)<\/p>\n<p>data = []<\/p>\n<p>for show in tv_shows:<\/p>\n<p>    title = show.find_element(By.CLASS_NAME, &#39;title&#39;).text<\/p>\n<p>    link = show.find_element(By.TAG_NAME, &#39;a&#39;).get_attribute(&#39;href&#39;)<\/p>\n<p>    data.append([title, link])<\/p>\n<h2><strong>\u5b58\u50a8\u6570\u636e\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;tv_shows.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&#39;Title&#39;, &#39;URL&#39;])<\/p>\n<p>    writer.writerows(data)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p>print(&#39;Data saved to tv_shows.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u53cd\u722c\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u4e00\u4e9b\u7f51\u7ad9\u4f1a\u91c7\u53d6\u53cd\u722c\u673a\u5236\uff0c\u5982\u9a8c\u8bc1\u7801\u3001IP\u5c01\u7981\u7b49\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e9b\u53cd\u722c\u673a\u5236\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u6a21\u62df\u7528\u6237\u884c\u4e3a<\/h4>\n<\/p>\n<p><p>\u6a21\u62df\u7528\u6237\u884c\u4e3a\u53ef\u4ee5\u4f7f\u722c\u866b\u884c\u4e3a\u66f4\u52a0\u81ea\u7136\uff0c\u907f\u514d\u88ab\u8bc6\u522b\u4e3a\u81ea\u52a8\u5316\u7a0b\u5e8f\u3002\u53ef\u4ee5\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u6765\u6a21\u62df\u9f20\u6807\u70b9\u51fb\u3001\u6eda\u52a8\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium.webdriver.common.action_ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns import ActionChains<\/p>\n<h2><strong>\u6a21\u62df\u9f20\u6807\u6eda\u52a8<\/strong><\/h2>\n<p>actions = ActionChains(driver)<\/p>\n<p>actions.move_to_element(tv_shows[-1]).perform()<\/p>\n<p>time.sleep(2)<\/p>\n<h2><strong>\u6a21\u62df\u9f20\u6807\u70b9\u51fb<\/strong><\/h2>\n<p>button = driver.find_element(By.CLASS_NAME, &#39;next_page&#39;)<\/p>\n<p>button.click()<\/p>\n<p>time.sleep(5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u89e3\u51b3\u9a8c\u8bc1\u7801<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u9700\u8981\u8f93\u5165\u9a8c\u8bc1\u7801\u7684\u7f51\u7ad9\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\uff0c\u5982\u6253\u7801\u5e73\u53f0\uff0c\u6216\u624b\u52a8\u8f93\u5165\u9a8c\u8bc1\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u624b\u52a8\u8f93\u5165\u9a8c\u8bc1\u7801<\/p>\n<p>captcha = input(&#39;Please enter the captcha: &#39;)<\/p>\n<p>captcha_input = driver.find_element(By.ID, &#39;captcha_input&#39;)<\/p>\n<p>captcha_input.send_keys(captcha)<\/p>\n<p>submit_button = driver.find_element(By.ID, &#39;submit_button&#39;)<\/p>\n<p>submit_button.click()<\/p>\n<p>time.sleep(5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7528Python\u83b7\u53d6\u7f51\u4e0a\u7684\u5168\u90e8\u7535\u89c6\u5267\u4fe1\u606f\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u6269\u5c55\u548c\u4f18\u5316\uff0c\u5982\u6dfb\u52a0\u66f4\u591a\u6570\u636e\u5b57\u6bb5\u3001\u5904\u7406\u66f4\u591a\u7f51\u7ad9\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6dfb\u52a0\u66f4\u591a\u6570\u636e\u5b57\u6bb5<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6839\u636e\u9700\u6c42\uff0c\u63d0\u53d6\u66f4\u591a\u7684\u7535\u89c6\u5267\u4fe1\u606f\uff0c\u5982\u5bfc\u6f14\u3001\u6f14\u5458\u3001\u8bc4\u5206\u3001\u7b80\u4ecb\u7b49\u3002\u5728\u7f16\u5199\u722c\u866b\u4ee3\u7801\u65f6\uff0c\u589e\u52a0\u76f8\u5e94\u7684\u63d0\u53d6\u903b\u8f91\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for show in tv_shows:<\/p>\n<p>    title = show.find_element(By.CLASS_NAME, &#39;title&#39;).text<\/p>\n<p>    link = show.find_element(By.TAG_NAME, &#39;a&#39;).get_attribute(&#39;href&#39;)<\/p>\n<p>    director = show.find_element(By.CLASS_NAME, &#39;director&#39;).text<\/p>\n<p>    rating = show.find_element(By.CLASS_NAME, &#39;rating&#39;).text<\/p>\n<p>    data.append([title, link, director, rating])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u66f4\u591a\u7f51\u7ad9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7f51\u7ad9\u7684\u7ed3\u6784\uff0c\u7f16\u5199\u4e0d\u540c\u7684\u722c\u866b\u4ee3\u7801\u6765\u6293\u53d6\u6570\u636e\u3002\u53ef\u4ee5\u5c06\u722c\u866b\u4ee3\u7801\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u65b9\u4fbf\u8c03\u7528\u548c\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def scrape_tv_shows(url):<\/p>\n<p>    response = requests.get(url, headers=headers)<\/p>\n<p>    html_content = response.text<\/p>\n<p>    soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p>    tv_shows = soup.find_all(&#39;div&#39;, class_=&#39;tv_show&#39;)<\/p>\n<p>    data = []<\/p>\n<p>    for show in tv_shows:<\/p>\n<p>        title = show.find(&#39;h2&#39;, class_=&#39;title&#39;).get_text()<\/p>\n<p>        link = show.find(&#39;a&#39;)[&#39;href&#39;]<\/p>\n<p>        data.append([title, link])<\/p>\n<p>    return data<\/p>\n<h2><strong>\u6293\u53d6\u591a\u4e2a\u7f51\u7ad9\u7684\u6570\u636e<\/strong><\/h2>\n<p>urls = [&#39;https:\/\/example.com\/tv_shows1&#39;, &#39;https:\/\/example.com\/tv_shows2&#39;]<\/p>\n<p>all_data = []<\/p>\n<p>for url in urls:<\/p>\n<p>    data = scrape_tv_shows(url)<\/p>\n<p>    all_data.extend(data)<\/p>\n<h2><strong>\u5b58\u50a8\u6570\u636e\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;tv_shows.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&#39;Title&#39;, &#39;URL&#39;])<\/p>\n<p>    writer.writerows(all_data)<\/p>\n<p>print(&#39;Data saved to tv_shows.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528Python\u9ad8\u6548\u5730\u83b7\u53d6\u7f51\u4e0a\u7684\u5168\u90e8\u7535\u89c6\u5267\u4fe1\u606f\uff0c\u5e76\u8fdb\u884c\u5b58\u50a8\u548c\u5206\u6790\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u722c\u53d6\u7535\u89c6\u5267\u6570\u636e\uff1f<\/strong><br \/>\u722c\u53d6\u7535\u89c6\u5267\u6570\u636e\u901a\u5e38\u9700\u8981\u4f7f\u7528Python\u5e93\uff0c\u5982Requests\u548cBeautifulSoup\u3002\u901a\u8fc7Requests\u5e93\u53ef\u4ee5\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u800cBeautifulSoup\u5219\u5e2e\u52a9\u89e3\u6790HTML\u7ed3\u6784\u3002\u786e\u4fdd\u9075\u5faa\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\uff0c\u907f\u514d\u5bf9\u670d\u52a1\u5668\u9020\u6210\u8fc7\u5927\u538b\u529b\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u53ef\u7528\u7684API\u53ef\u4ee5\u83b7\u53d6\u7535\u89c6\u5267\u4fe1\u606f\uff1f<\/strong><br \/>\u8bb8\u591a\u7f51\u7ad9\u63d0\u4f9bAPI\u6765\u83b7\u53d6\u7535\u89c6\u5267\u4fe1\u606f\uff0c\u4f8b\u5982The Movie Database (TMDb) API\u548cOpen Movie Database (OMDb) API\u3002\u8fd9\u4e9bAPI\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u7b80\u5355\u7684HTTP\u8bf7\u6c42\u83b7\u53d6\u6700\u65b0\u7684\u7535\u89c6\u5267\u6570\u636e\uff0c\u5305\u62ec\u5267\u96c6\u3001\u8bc4\u5206\u548c\u6f14\u5458\u4fe1\u606f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u83b7\u53d6\u7535\u89c6\u5267\u6570\u636e\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u722c\u53d6\u6570\u636e\u65f6\uff0c\u7528\u6237\u9700\u9075\u5faa\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u5c24\u5176\u662f\u7248\u6743\u95ee\u9898\u3002\u786e\u4fdd\u4e0d\u8fdd\u53cd\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u907f\u514d\u722c\u53d6\u53d7\u7248\u6743\u4fdd\u62a4\u7684\u5185\u5bb9\u3002\u6700\u597d\u4f7f\u7528\u516c\u5f00\u7684API\uff0c\u6216\u83b7\u53d6\u7f51\u7ad9\u7684\u6388\u6743\uff0c\u786e\u4fdd\u5408\u6cd5\u5408\u89c4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u83b7\u53d6\u7f51\u4e0a\u7684\u5168\u90e8\u7535\u89c6\u5267 \u8981\u7528Python\u83b7\u53d6\u7f51\u4e0a\u7684\u5168\u90e8\u7535\u89c6\u5267\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u722c\u866b\u3001API\u8c03\u7528\u3001\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1133213,"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\/1133203"}],"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=1133203"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133203\/revisions"}],"predecessor-version":[{"id":1133214,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133203\/revisions\/1133214"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133213"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}