{"id":1061063,"date":"2024-12-31T15:39:37","date_gmt":"2024-12-31T07:39:37","guid":{"rendered":""},"modified":"2024-12-31T15:39:40","modified_gmt":"2024-12-31T07:39:40","slug":"python%e5%a6%82%e4%bd%95%e7%88%ac%e5%8f%96%e7%bd%91%e9%a1%b5%e8%a1%a8%e6%a0%bc%e7%9a%84%e4%b8%bb%e4%bd%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1061063.html","title":{"rendered":"python\u5982\u4f55\u722c\u53d6\u7f51\u9875\u8868\u683c\u7684\u4e3b\u4f53"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/b437b7e7-25d8-45e7-be29-3d7af9d2b751.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u722c\u53d6\u7f51\u9875\u8868\u683c\u7684\u4e3b\u4f53\" \/><\/p>\n<p><p> <strong>\u722c\u53d6\u7f51\u9875\u8868\u683c\u7684\u4e3b\u4f53\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>requests<\/code>\u3001<code>BeautifulSoup<\/code>\u3001<code>pandas<\/code>\u7b49\u5e93\uff0c\u901a\u8fc7\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790HTML\u3001\u63d0\u53d6\u8868\u683c\u6570\u636e\u7b49\u6b65\u9aa4\u5b9e\u73b0\u3002<\/strong> \u9996\u5148\uff0c\u4f7f\u7528<code>requests<\/code>\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875HTML\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528<code>BeautifulSoup<\/code>\u89e3\u6790HTML\uff0c\u627e\u5230\u8868\u683c\u5143\u7d20\u5e76\u63d0\u53d6\u8868\u683c\u6570\u636e\uff0c\u6700\u540e\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u5c06\u8868\u683c\u6570\u636e\u8f6c\u6362\u4e3aDataFrame\u8fdb\u884c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875HTML<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u53d1\u9001HTTP\u8bf7\u6c42\u6765\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<code>requests<\/code>\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684HTTP\u5e93\uff0c\u53ef\u4ee5\u53d1\u9001GET\u3001POST\u7b49\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;https:\/\/example.com\/tablepage&quot;  # \u66ff\u6362\u4e3a\u5b9e\u9645\u7f51\u9875URL<\/p>\n<p>response = requests.get(url)<\/p>\n<p>html_content = response.content<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests.get<\/code>\u65b9\u6cd5\u53d1\u9001GET\u8bf7\u6c42\uff0c\u5e76\u5c06\u54cd\u5e94\u5185\u5bb9\u5b58\u50a8\u5728<code>html_content<\/code>\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u89e3\u6790HTML\u627e\u5230\u8868\u683c\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u89e3\u6790HTML\u5185\u5bb9\u5e76\u627e\u5230\u5305\u542b\u8868\u683c\u6570\u636e\u7684\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<code>BeautifulSoup<\/code>\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684HTML\u548cXML\u89e3\u6790\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6570\u636e\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>table = soup.find(&#39;table&#39;)  # \u627e\u5230\u7b2c\u4e00\u4e2a\u8868\u683c\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>BeautifulSoup<\/code>\u89e3\u6790HTML\u5185\u5bb9\uff0c\u5e76\u627e\u5230\u7b2c\u4e00\u4e2a\u8868\u683c\u5143\u7d20\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u4f7f\u7528\u4e0d\u540c\u7684\u9009\u62e9\u5668\u6765\u5b9a\u4f4d\u7279\u5b9a\u7684\u8868\u683c\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u63d0\u53d6\u8868\u683c\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u627e\u5230\u8868\u683c\u5143\u7d20\u540e\uff0c\u6211\u4eec\u9700\u8981\u63d0\u53d6\u8868\u683c\u4e2d\u7684\u6570\u636e\u3002\u53ef\u4ee5\u904d\u5386\u8868\u683c\u7684\u884c\u548c\u5355\u5143\u683c\uff0c\u63d0\u53d6\u6bcf\u4e2a\u5355\u5143\u683c\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = table.find_all(&#39;tr&#39;)<\/p>\n<p>table_data = []<\/p>\n<p>for row in rows:<\/p>\n<p>    cells = row.find_all([&#39;td&#39;, &#39;th&#39;])<\/p>\n<p>    row_data = [cell.get_text(strip=True) for cell in cells]<\/p>\n<p>    table_data.append(row_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>find_all<\/code>\u65b9\u6cd5\u627e\u5230\u8868\u683c\u4e2d\u7684\u6240\u6709\u884c\uff0c\u5e76\u904d\u5386\u6bcf\u4e00\u884c\uff0c\u63d0\u53d6\u6bcf\u4e2a\u5355\u5143\u683c\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002\u63d0\u53d6\u5230\u7684\u8868\u683c\u6570\u636e\u5b58\u50a8\u5728<code>table_data<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8f6c\u6362\u4e3aDataFrame\u8fdb\u884c\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u65b9\u4fbf\u5904\u7406\u548c\u5206\u6790\u8868\u683c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u5c06\u6570\u636e\u8f6c\u6362\u4e3aDataFrame\u683c\u5f0f\u3002<code>pandas<\/code>\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame(table_data[1:], columns=table_data[0])  # \u5c06\u6570\u636e\u8f6c\u6362\u4e3aDataFrame\uff0c\u5e76\u8bbe\u7f6e\u5217\u540d<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pandas.DataFrame<\/code>\u65b9\u6cd5\u5c06\u8868\u683c\u6570\u636e\u8f6c\u6362\u4e3aDataFrame\u683c\u5f0f\uff0c\u5e76\u8bbe\u7f6e\u5217\u540d\u3002\u53ef\u4ee5\u4f7f\u7528<code>print<\/code>\u65b9\u6cd5\u6253\u5370DataFrame\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u548c\u5206\u6790\u8868\u683c\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u8f6c\u6362\u4e3aDataFrame\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u63d0\u4f9b\u7684\u5404\u79cd\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fc7\u6ee4\u6570\u636e\u3001\u8ba1\u7b97\u7edf\u8ba1\u91cf\u3001\u751f\u6210\u56fe\u8868\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u8ba1\u7b97\u6bcf\u5217\u7684\u5e73\u5747\u503c<\/p>\n<p>mean_values = df.mean()<\/p>\n<p>print(mean_values)<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u8fc7\u6ee4\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u636e<\/strong><\/h2>\n<p>filtered_data = df[df[&#39;column_name&#39;] &gt; threshold_value]<\/p>\n<p>print(filtered_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6f14\u793a\u4e86\u5982\u4f55\u8ba1\u7b97\u6bcf\u5217\u7684\u5e73\u5747\u503c\uff0c\u4ee5\u53ca\u5982\u4f55\u8fc7\u6ee4\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u636e\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u4f7f\u7528<code>pandas<\/code>\u5e93\u63d0\u4f9b\u7684\u5404\u79cd\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b58\u50a8\u548c\u5bfc\u51fa\u8868\u683c\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u548c\u5206\u6790\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5c06\u8868\u683c\u6570\u636e\u5b58\u50a8\u548c\u5bfc\u51fa\u4e3a\u5404\u79cd\u683c\u5f0f\uff0c\u4f8b\u5982CSV\u3001Excel\u3001SQL\u7b49\u3002<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u6570\u636e\u5bfc\u51fa\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bfc\u51fa\u4e3aCSV\u6587\u4ef6<\/p>\n<p>df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<h2><strong>\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<h2><strong>\u5bfc\u51fa\u4e3aSQL\u8868<\/strong><\/h2>\n<p>from sqlalchemy import create_engine<\/p>\n<p>engine = create_engine(&#39;sqlite:\/\/\/database.db&#39;)<\/p>\n<p>df.to_sql(&#39;table_name&#39;, con=engine, if_exists=&#39;replace&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6f14\u793a\u4e86\u5982\u4f55\u5c06DataFrame\u6570\u636e\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u4ee5\u53ca\u5b58\u50a8\u5230SQL\u8868\u4e2d\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u5bfc\u51fa\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u52a8\u6001\u7f51\u9875<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u52a8\u6001\u52a0\u8f7d\u5185\u5bb9\u7684\u7f51\u9875\uff08\u4f8b\u5982\u901a\u8fc7JavaScript\u52a0\u8f7d\u8868\u683c\u6570\u636e\uff09\uff0c\u9700\u8981\u4f7f\u7528Selenium\u5e93\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u52a0\u8f7d\u7f51\u9875\u5185\u5bb9\u540e\u518d\u63d0\u53d6\u8868\u683c\u6570\u636e\u3002Selenium\u5e93\u53ef\u4ee5\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u652f\u6301\u591a\u79cd\u6d4f\u89c8\u5668\u9a71\u52a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u8bbe\u7f6e\u6d4f\u89c8\u5668\u9a71\u52a8<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;path\/to\/chromedriver&#39;)<\/p>\n<h2><strong>\u52a0\u8f7d\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/example.com\/tablepage&quot;)<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\uff08\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u7b49\u5f85\u65f6\u95f4\u6216\u4f7f\u7528\u663e\u5f0f\u7b49\u5f85\uff09<\/strong><\/h2>\n<p>import time<\/p>\n<p>time.sleep(5)<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875HTML\u5185\u5bb9<\/strong><\/h2>\n<p>html_content = driver.page_source<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<h2><strong>\u89e3\u6790HTML\u5e76\u63d0\u53d6\u8868\u683c\u6570\u636e\uff08\u540c\u4e0a\uff09<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p>table = soup.find(&#39;table&#39;)<\/p>\n<p>rows = table.find_all(&#39;tr&#39;)<\/p>\n<p>table_data = []<\/p>\n<p>for row in rows:<\/p>\n<p>    cells = row.find_all([&#39;td&#39;, &#39;th&#39;])<\/p>\n<p>    row_data = [cell.get_text(strip=True) for cell in cells]<\/p>\n<p>    table_data.append(row_data)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(table_data[1:], columns=table_data[0])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Selenium\u5e93\u52a0\u8f7d\u7f51\u9875\u5185\u5bb9\uff0c\u5e76\u63d0\u53d6\u8868\u683c\u6570\u636e\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u7b49\u5f85\u65f6\u95f4\u6216\u4f7f\u7528\u663e\u5f0f\u7b49\u5f85\u6765\u786e\u4fdd\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u590d\u6742\u8868\u683c\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7ed3\u6784\u7684\u8868\u683c\uff08\u4f8b\u5982\u5d4c\u5957\u8868\u683c\u3001\u8de8\u884c\u8de8\u5217\u5355\u5143\u683c\u7b49\uff09\uff0c\u9700\u8981\u4f7f\u7528\u66f4\u590d\u6742\u7684\u89e3\u6790\u903b\u8f91\u6765\u63d0\u53d6\u8868\u683c\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u89e3\u6790\u5d4c\u5957\u8868\u683c\uff0c\u6216\u5904\u7406\u8de8\u884c\u8de8\u5217\u7684\u5355\u5143\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def parse_table(table):<\/p>\n<p>    rows = table.find_all(&#39;tr&#39;)<\/p>\n<p>    table_data = []<\/p>\n<p>    for row in rows:<\/p>\n<p>        cells = row.find_all([&#39;td&#39;, &#39;th&#39;])<\/p>\n<p>        row_data = [cell.get_text(strip=True) for cell in cells]<\/p>\n<p>        # \u5904\u7406\u5d4c\u5957\u8868\u683c<\/p>\n<p>        nested_tables = row.find_all(&#39;table&#39;)<\/p>\n<p>        for nested_table in nested_tables:<\/p>\n<p>            nested_table_data = parse_table(nested_table)<\/p>\n<p>            row_data.append(nested_table_data)<\/p>\n<p>        table_data.append(row_data)<\/p>\n<p>    return table_data<\/p>\n<h2><strong>\u89e3\u6790\u5d4c\u5957\u8868\u683c<\/strong><\/h2>\n<p>table_data = parse_table(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570<code>parse_table<\/code>\u6765\u89e3\u6790\u5d4c\u5957\u8868\u683c\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u89e3\u6790\u903b\u8f91\uff0c\u5904\u7406\u8de8\u884c\u8de8\u5217\u7684\u5355\u5143\u683c\u7b49\u590d\u6742\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u7f51\u9875\u53cd\u722c\u866b\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u4e00\u4e9b\u7f51\u7ad9\u53ef\u80fd\u4f1a\u6709\u53cd\u722c\u866b\u673a\u5236\uff0c\u4f8b\u5982\u901a\u8fc7\u9a8c\u8bc1\u7801\u3001IP\u5c01\u7981\u3001\u52a8\u6001\u5185\u5bb9\u52a0\u8f7d\u7b49\u65b9\u5f0f\u9650\u5236\u722c\u866b\u8bbf\u95ee\u3002\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b56\u7565\u6765\u5e94\u5bf9\u53cd\u722c\u866b\u673a\u5236\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u4ee3\u7406IP\uff1a<\/strong> \u53ef\u4ee5\u4f7f\u7528\u4ee3\u7406IP\u6765\u907f\u514dIP\u5c01\u7981\u3002<\/li>\n<li><strong>\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff1a<\/strong> \u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u6a21\u62df\u771f\u5b9e\u7528\u6237\u7684\u6d4f\u89c8\u5668\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u5904\u7406\u9a8c\u8bc1\u7801\uff1a<\/strong> \u53ef\u4ee5\u4f7f\u7528OCR\u6280\u672f\u6216\u4eba\u5de5\u65b9\u5f0f\u5904\u7406\u9a8c\u8bc1\u7801\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff1a<\/strong> \u53ef\u4ee5\u8bbe\u7f6eHTTP\u8bf7\u6c42\u5934\u6765\u6a21\u62df\u771f\u5b9e\u6d4f\u89c8\u5668\u8bf7\u6c42\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528\u4ee3\u7406IP<\/p>\n<p>proxies = {<\/p>\n<p>    &#39;http&#39;: &#39;http:\/\/username:password@proxyserver:port&#39;,<\/p>\n<p>    &#39;https&#39;: &#39;http:\/\/username:password@proxyserver:port&#39;<\/p>\n<p>}<\/p>\n<p>response = requests.get(url, proxies=proxies)<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u8bbe\u7f6e\u8bf7\u6c42\u5934<\/strong><\/h2>\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>response = requests.get(url, headers=headers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4ee3\u7406IP\u548c\u8bbe\u7f6e\u8bf7\u6c42\u5934\u6765\u5e94\u5bf9\u53cd\u722c\u866b\u673a\u5236\u3002\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u722c\u53d6\u7f51\u9875\u8868\u683c\u7684\u4e3b\u4f53\uff0c\u5e76\u5bf9\u8868\u683c\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\u3002\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875HTML\u3001\u89e3\u6790HTML\u627e\u5230\u8868\u683c\u5143\u7d20\u3001\u63d0\u53d6\u8868\u683c\u6570\u636e\u3001\u8f6c\u6362\u4e3aDataFrame\u8fdb\u884c\u5904\u7406\u3001\u5b58\u50a8\u548c\u5bfc\u51fa\u8868\u683c\u6570\u636e\u3002\u5bf9\u4e8e\u52a8\u6001\u7f51\u9875\u548c\u590d\u6742\u8868\u683c\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u548c\u9012\u5f52\u89e3\u6790\u7b49\u6280\u672f\u6765\u5904\u7406\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b56\u7565\u5e94\u5bf9\u7f51\u9875\u7684\u53cd\u722c\u866b\u673a\u5236\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u4e86\u89e3\u548c\u5b9e\u73b0Python\u722c\u53d6\u7f51\u9875\u8868\u683c\u7684\u4e3b\u4f53\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\u63d0\u53d6\u7f51\u9875\u8868\u683c\u4e2d\u7684\u6570\u636e\uff1f<\/strong><br \/>\u4f7f\u7528Python\u63d0\u53d6\u7f51\u9875\u8868\u683c\u6570\u636e\u7684\u5e38\u7528\u5e93\u5305\u62ecBeautiful Soup\u548cPandas\u3002\u9996\u5148\uff0c\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875HTML\u5185\u5bb9\uff0c\u7136\u540e\u5229\u7528Beautiful Soup\u89e3\u6790HTML\uff0c\u627e\u5230\u8868\u683c\u5143\u7d20\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u901a\u8fc7Pandas\u5c06\u63d0\u53d6\u7684\u6570\u636e\u8f6c\u6362\u4e3aDataFrame\uff0c\u65b9\u4fbf\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u8bfb\u53d6\u7f51\u9875\u3001\u89e3\u6790HTML\u3001\u5b9a\u4f4d\u8868\u683c\u5e76\u63d0\u53d6\u5185\u5bb9\u3001\u6700\u540e\u5c06\u6570\u636e\u5b58\u50a8\u4e3aCSV\u6216Excel\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u7f51\u9875\u8868\u683c\u65f6\uff0c\u5982\u4f55\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8bb8\u591a\u7f51\u9875\u8868\u683c\u7684\u6570\u636e\u662f\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u7684\uff0c\u4f7f\u7528requests\u5e93\u53ef\u80fd\u65e0\u6cd5\u76f4\u63a5\u83b7\u53d6\u8fd9\u4e9b\u6570\u636e\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u7b49\u5f85\u9875\u9762\u5b8c\u5168\u52a0\u8f7d\u540e\u518d\u63d0\u53d6\u6240\u9700\u7684\u8868\u683c\u6570\u636e\u3002\u901a\u8fc7\u8bbe\u7f6e\u9002\u5f53\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u53ef\u4ee5\u786e\u4fdd\u6293\u53d6\u5230\u5b8c\u6574\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5fc5\u8981\u9075\u5faa\u7f51\u9875\u722c\u866b\u7684\u9053\u5fb7\u89c4\u8303\uff1f<\/strong><br \/>\u5728\u722c\u53d6\u7f51\u9875\u6570\u636e\u65f6\uff0c\u9075\u5faa\u9053\u5fb7\u89c4\u8303\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8fd9\u5305\u62ec\u5c0a\u91cd\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\uff0c\u4e86\u89e3\u54ea\u4e9b\u5185\u5bb9\u5141\u8bb8\u6293\u53d6\uff0c\u907f\u514d\u5bf9\u670d\u52a1\u5668\u9020\u6210\u8fc7\u5927\u8d1f\u62c5\uff0c\u5408\u7406\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694\u7b49\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4e0d\u4fb5\u72af\u7248\u6743\u548c\u9690\u79c1\u6743\uff0c\u5408\u6cd5\u4f7f\u7528\u83b7\u53d6\u7684\u6570\u636e\uff0c\u90fd\u662f\u8fdb\u884c\u7f51\u9875\u722c\u866b\u65f6\u9700\u8981\u8003\u8651\u7684\u56e0\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u722c\u53d6\u7f51\u9875\u8868\u683c\u7684\u4e3b\u4f53\u53ef\u4ee5\u4f7f\u7528Python\u7684requests\u3001BeautifulSoup\u3001pandas\u7b49\u5e93\uff0c\u901a\u8fc7\u53d1 [&hellip;]","protected":false},"author":3,"featured_media":1061070,"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\/1061063"}],"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=1061063"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061063\/revisions"}],"predecessor-version":[{"id":1061071,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061063\/revisions\/1061071"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1061070"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1061063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1061063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1061063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}