{"id":1103703,"date":"2025-01-08T16:13:12","date_gmt":"2025-01-08T08:13:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1103703.html"},"modified":"2025-01-08T16:13:16","modified_gmt":"2025-01-08T08:13:16","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%8a%93%e5%8f%9658%e6%8b%9b%e8%81%98%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1103703.html","title":{"rendered":"\u5982\u4f55\u7528python\u6293\u53d658\u62db\u8058\u4fe1\u606f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065414\/93060cc6-a29b-4beb-87f3-99f7c433c0b5.webp\" alt=\"\u5982\u4f55\u7528python\u6293\u53d658\u62db\u8058\u4fe1\u606f\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u6293\u53d658\u62db\u8058\u4fe1\u606f<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u7528Python\u6293\u53d658\u62db\u8058\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>Requests\u5e93\u3001BeautifulSoup\u5e93\u3001Selenium\u5e93\u3001\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a<\/strong>\u7b49\u6280\u672f\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u5b9e\u73b0\u5bf958\u540c\u57ce\u62db\u8058\u4fe1\u606f\u7684\u722c\u53d6\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u6293\u53d658\u540c\u57ce\u7684\u62db\u8058\u4fe1\u606f\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u51c6\u5907\u597d\u76f8\u5173\u7684\u5f00\u53d1\u5de5\u5177\u548c\u73af\u5883\u3002\u4ee5\u4e0b\u662f\u6240\u9700\u7684\u5de5\u5177\u548c\u5e93\uff1a<\/p>\n<\/p>\n<ol>\n<li>Python 3.x<\/li>\n<li>Requests\u5e93\uff1a\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42<\/li>\n<li>BeautifulSoup\u5e93\uff1a\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863<\/li>\n<li>Selenium\u5e93\uff1a\u7528\u4e8e\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a<\/li>\n<li>ChromeDriver\uff1a\u7528\u4e8e\u9a71\u52a8Chrome\u6d4f\u89c8\u5668<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p>pip install beautifulsoup4<\/p>\n<p>pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u83b7\u53d6\u9875\u9762\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u786e\u5b9a\u8981\u6293\u53d6\u7684\u9875\u9762URL\uff0c\u5e76\u4f7f\u7528Requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u9875\u9762\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u83b7\u53d658\u540c\u57ce\u62db\u8058\u9875\u9762\u7684HTML\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;https:\/\/www.58.com\/zhaopin\/&#39;<\/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\/58.0.3029.110 Safari\/537.3&#39;}<\/p>\n<p>response = requests.get(url, headers=headers)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    page_content = response.text<\/p>\n<p>    print(page_content)<\/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 page content: {response.status_code}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u5e93\u89e3\u6790\u83b7\u53d6\u5230\u7684HTML\u5185\u5bb9\uff0c\u5e76\u63d0\u53d6\u62db\u8058\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u63d0\u53d6\u62db\u8058\u804c\u4f4d\u540d\u79f0\u3001\u516c\u53f8\u540d\u79f0\u548c\u5de5\u8d44\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(page_content, &#39;html.parser&#39;)<\/p>\n<p>jobs = soup.find_all(&#39;div&#39;, class_=&#39;job_item&#39;)<\/p>\n<p>for job in jobs:<\/p>\n<p>    job_title = job.find(&#39;span&#39;, class_=&#39;job_name&#39;).text<\/p>\n<p>    company_name = job.find(&#39;a&#39;, class_=&#39;company_name&#39;).text<\/p>\n<p>    salary = job.find(&#39;p&#39;, class_=&#39;job_salary&#39;).text<\/p>\n<p>    print(f&quot;Job Title: {job_title}, Company: {company_name}, Salary: {salary}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u52a8\u6001\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u9875\u5185\u5bb9\u662f\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u7684\uff0c\u8fd9\u65f6\u9700\u8981\u4f7f\u7528Selenium\u5e93\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u4ee5\u4fbf\u83b7\u53d6\u5b8c\u6574\u7684\u9875\u9762\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Selenium\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u62db\u8058\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.chrome.service import Service<\/p>\n<p>from selenium.webdriver.common.by import By<\/p>\n<p>service = Service(&#39;path\/to\/chromedriver&#39;)<\/p>\n<p>driver = webdriver.Chrome(service=service)<\/p>\n<p>driver.get(&#39;https:\/\/www.58.com\/zhaopin\/&#39;)<\/p>\n<p>driver.implicitly_wait(10)<\/p>\n<p>jobs = driver.find_elements(By.CLASS_NAME, &#39;job_item&#39;)<\/p>\n<p>for job in jobs:<\/p>\n<p>    job_title = job.find_element(By.CLASS_NAME, &#39;job_name&#39;).text<\/p>\n<p>    company_name = job.find_element(By.CLASS_NAME, &#39;company_name&#39;).text<\/p>\n<p>    salary = job.find_element(By.CLASS_NAME, &#39;job_salary&#39;).text<\/p>\n<p>    print(f&quot;Job Title: {job_title}, Company: {company_name}, Salary: {salary}&quot;)<\/p>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b58\u50a8\u6293\u53d6\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u65b9\u4fbf\u540e\u7eed\u5206\u6790\u548c\u5904\u7406\uff0c\u53ef\u4ee5\u5c06\u6293\u53d6\u5230\u7684\u62db\u8058\u4fe1\u606f\u5b58\u50a8\u5230CSV\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c06\u6570\u636e\u5b58\u50a8\u5230CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = []<\/p>\n<p>for job in jobs:<\/p>\n<p>    job_title = job.find_element(By.CLASS_NAME, &#39;job_name&#39;).text<\/p>\n<p>    company_name = job.find_element(By.CLASS_NAME, &#39;company_name&#39;).text<\/p>\n<p>    salary = job.find_element(By.CLASS_NAME, &#39;job_salary&#39;).text<\/p>\n<p>    data.append([job_title, company_name, salary])<\/p>\n<p>with open(&#39;jobs.csv&#39;, &#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&#39;Job Title&#39;, &#39;Company&#39;, &#39;Salary&#39;])<\/p>\n<p>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u53cd\u722c\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u6293\u53d658\u540c\u57ce\u62db\u8058\u4fe1\u606f\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u53cd\u722c\u673a\u5236\u7684\u963b\u788d\u3002\u4e3a\u4e86\u907f\u514d\u88ab\u5c01\u7981\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbe\u7f6e\u8bf7\u6c42\u5934\u90e8\u4fe1\u606f<\/strong>\uff1a\u6a21\u62df\u6b63\u5e38\u7528\u6237\u7684\u6d4f\u89c8\u5668\u8bf7\u6c42\u3002<\/li>\n<li><strong>\u4f7f\u7528\u4ee3\u7406IP<\/strong>\uff1a\u901a\u8fc7\u4ee3\u7406IP\u8f6e\u6362\u6765\u9690\u85cf\u771f\u5b9eIP\u5730\u5740\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694<\/strong>\uff1a\u907f\u514d\u9891\u7e41\u8bf7\u6c42\u89e6\u53d1\u53cd\u722c\u673a\u5236\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u4ee3\u7406IP\u548c\u8bf7\u6c42\u95f4\u9694\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import random<\/p>\n<p>from requests.exceptions import ProxyError<\/p>\n<p>proxies = [<\/p>\n<p>    &#39;http:\/\/proxy1.com&#39;,<\/p>\n<p>    &#39;http:\/\/proxy2.com&#39;,<\/p>\n<p>    &#39;http:\/\/proxy3.com&#39;<\/p>\n<p>]<\/p>\n<p>def get_random_proxy():<\/p>\n<p>    return random.choice(proxies)<\/p>\n<p>for i in range(10):<\/p>\n<p>    proxy = get_random_proxy()<\/p>\n<p>    try:<\/p>\n<p>        response = requests.get(url, headers=headers, proxies={&#39;http&#39;: proxy, &#39;https&#39;: proxy})<\/p>\n<p>        if response.status_code == 200:<\/p>\n<p>            page_content = response.text<\/p>\n<p>            # \u89e3\u6790\u5e76\u5904\u7406\u6570\u636e<\/p>\n<p>        else:<\/p>\n<p>            print(f&quot;Failed to retrieve page content: {response.status_code}&quot;)<\/p>\n<p>    except ProxyError:<\/p>\n<p>        print(&quot;Proxy error. Retrying with a different proxy.&quot;)<\/p>\n<p>    time.sleep(random.uniform(1, 5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u6293\u53d658\u540c\u57ce\u7684\u62db\u8058\u4fe1\u606f\u3002\u4e3b\u8981\u4f7f\u7528\u4e86<strong>Requests\u5e93\u3001BeautifulSoup\u5e93\u3001Selenium\u5e93\u3001\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a<\/strong>\u7b49\u6280\u672f\uff0c\u6db5\u76d6\u4e86\u83b7\u53d6\u9875\u9762\u6570\u636e\u3001\u89e3\u6790HTML\u5185\u5bb9\u3001\u5904\u7406\u52a8\u6001\u5185\u5bb9\u3001\u5b58\u50a8\u6293\u53d6\u7684\u6570\u636e\u4ee5\u53ca\u5904\u7406\u53cd\u722c\u673a\u5236\u7b49\u65b9\u9762\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6293\u53d6\u5e76\u4fdd\u5b5858\u540c\u57ce\u7684\u62db\u8058\u4fe1\u606f\uff0c\u4e3a\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u63d0\u4f9b\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><p>\u6293\u53d6\u6570\u636e\u65f6\u9700\u8981\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u5408\u7406\u4f7f\u7528\u6293\u53d6\u5de5\u5177\uff0c\u907f\u514d\u5bf9\u76ee\u6807\u7f51\u7ad9\u9020\u6210\u4e0d\u5fc5\u8981\u7684\u8d1f\u62c5\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\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\u6293\u53d658\u62db\u8058\u4fe1\u606f\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u6293\u53d658\u62db\u8058\u4fe1\u606f\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u76f8\u5173\u5e93\uff0c\u5982requests\u548cBeautifulSoup\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\uff0c\u63d0\u53d6\u9700\u8981\u7684\u62db\u8058\u4fe1\u606f\uff0c\u6bd4\u5982\u804c\u4f4d\u540d\u79f0\u3001\u516c\u53f8\u540d\u79f0\u3001\u85aa\u8d44\u7b49\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u5c06\u6293\u53d6\u5230\u7684\u6570\u636e\u5b58\u50a8\u5728CSV\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\u3002<\/p>\n<p><strong>\u6293\u53d658\u62db\u8058\u4fe1\u606f\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u548c\u9053\u5fb7\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u6293\u53d658\u62db\u8058\u4fe1\u606f\u65f6\uff0c\u91cd\u8981\u7684\u662f\u9075\u5faa\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u4e2d\u7684\u89c4\u5b9a\uff0c\u786e\u4fdd\u4e0d\u8fdd\u53cd\u7f51\u7ad9\u7684\u6293\u53d6\u653f\u7b56\u3002\u540c\u65f6\uff0c\u5c0a\u91cd\u6570\u636e\u9690\u79c1\u548c\u77e5\u8bc6\u4ea7\u6743\uff0c\u907f\u514d\u6293\u53d6\u4e2a\u4eba\u4fe1\u606f\u548c\u5546\u4e1a\u654f\u611f\u6570\u636e\u3002\u5408\u7406\u4f7f\u7528\u6293\u53d6\u7684\u6570\u636e\uff0c\u5e76\u9075\u5faa\u5f53\u5730\u7684\u6570\u636e\u4fdd\u62a4\u6cd5\u5f8b\uff0c\u5982GDPR\u6216CCPA\u7b49\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u6293\u53d658\u62db\u8058\u4fe1\u606f\u7684\u5e38\u89c1\u95ee\u9898\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728\u6293\u53d658\u62db\u8058\u4fe1\u606f\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25\u3001HTML\u7ed3\u6784\u53d8\u5316\u3001\u53cd\u722c\u866b\u673a\u5236\u7b49\u95ee\u9898\u3002\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25\u65f6\uff0c\u53ef\u4ee5\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\u6216\u8bf7\u6c42\u7684URL\u662f\u5426\u6b63\u786e\u3002HTML\u7ed3\u6784\u53d8\u5316\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u63d0\u53d6\u5931\u8d25\uff0c\u8fd9\u65f6\u9700\u8981\u66f4\u65b0\u89e3\u6790\u4ee3\u7801\u3002\u5bf9\u4e8e\u53cd\u722c\u866b\u673a\u5236\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u6dfb\u52a0\u8bf7\u6c42\u5934\u3001\u4f7f\u7528\u4ee3\u7406IP\u6216\u63a7\u5236\u8bf7\u6c42\u9891\u7387\u7b49\u65b9\u6cd5\u6765\u89c4\u907f\u9650\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u6293\u53d658\u62db\u8058\u4fe1\u606f \u8981\u7528Python\u6293\u53d658\u62db\u8058\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528Requests\u5e93\u3001Beauti [&hellip;]","protected":false},"author":3,"featured_media":1103712,"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\/1103703"}],"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=1103703"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103703\/revisions"}],"predecessor-version":[{"id":1103714,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103703\/revisions\/1103714"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1103712"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1103703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1103703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1103703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}