{"id":1174693,"date":"2025-01-15T17:21:56","date_gmt":"2025-01-15T09:21:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174693.html"},"modified":"2025-01-15T17:21:58","modified_gmt":"2025-01-15T09:21:58","slug":"python%e5%a6%82%e4%bd%95%e7%88%ac%e5%8f%96-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174693.html","title":{"rendered":"python\u5982\u4f55\u722c\u53d6<td class="},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110338\/d3ae27d2-58bb-478a-82a4-bde274a5ddcc.webp\" alt=\"python\u5982\u4f55\u722c\u53d6\n\n<td class=\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u722c\u53d6<code>&lt;td class=...&gt;<\/code>\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u901a\u8fc7BeautifulSoup\u3001Selenium\u3001Scrapy\u7b49\u5de5\u5177\u5b9e\u73b0\u3002<\/strong>\u3001<strong>\u5176\u4e2d\u4f7f\u7528BeautifulSoup\u662f\u6700\u5e38\u89c1\u548c\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00<\/strong>\u3001<strong>\u4f7f\u7528Selenium\u9002\u5408\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u7f51\u9875<\/strong>\u3001<strong>Scrapy\u9002\u5408\u5927\u89c4\u6a21\u722c\u53d6\u548c\u6570\u636e\u7ba1\u7406<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u722c\u53d6<code>&lt;td class=...&gt;<\/code>\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528BeautifulSoup\u722c\u53d6<\/p>\n<td class=...>\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2aPython\u5e93\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u8f7b\u677e\u5730\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528BeautifulSoup\u722c\u53d6\u7f51\u9875\u4e2d<code>&lt;td class=...&gt;<\/code>\u5185\u5bb9\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5BeautifulSoup\u548cRequests<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86BeautifulSoup\u548cRequests\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p>pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7f16\u5199\u722c\u53d6\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u7f16\u5199Python\u811a\u672c\u6765\u722c\u53d6\u7f51\u9875\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u6240\u6709&lt;td class=&quot;your-class-name&quot;&gt;<\/strong><\/h2>\n<p>td_elements = soup.find_all(&#39;td&#39;, class_=&#39;your-class-name&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u5185\u5bb9<\/strong><\/h2>\n<p>for td in td_elements:<\/p>\n<p>    print(td.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Selenium\u722c\u53d6<\/p>\n<td class=...>\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316Web\u6d4f\u89c8\u5668\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u7f51\u9875\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Selenium\u548cWebDriver<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Selenium\u5e93\uff0c\u5e76\u4e0b\u8f7d\u76f8\u5e94\u7684WebDriver\uff0c\u4f8b\u5982ChromeDriver\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u8f7dChromeDriver\u540e\uff0c\u5c06\u5176\u653e\u7f6e\u5728\u7cfb\u7edfPATH\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u5199\u722c\u53d6\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u7f16\u5199Python\u811a\u672c\u6765\u4f7f\u7528Selenium\u722c\u53d6\u7f51\u9875\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u8bbe\u7f6eWebDriver<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u67e5\u627e\u6240\u6709&lt;td class=&quot;your-class-name&quot;&gt;<\/strong><\/h2>\n<p>td_elements = driver.find_elements_by_css_selector(&#39;td.your-class-name&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u5185\u5bb9<\/strong><\/h2>\n<p>for td in td_elements:<\/p>\n<p>    print(td.text)<\/p>\n<h2><strong>\u5173\u95edWebDriver<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Scrapy\u722c\u53d6<\/p>\n<td class=...>\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u722c\u53d6\u548c\u6570\u636e\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Scrapy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Scrapy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scrapy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efaScrapy\u9879\u76ee<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2aScrapy\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scrapy startproject myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7f16\u5199\u722c\u53d6\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u7684spiders\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u722c\u866b\u6587\u4ef6\uff0c\u4f8b\u5982<code>myspider.py<\/code>\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>        td_elements = response.css(&#39;td.your-class-name&#39;)<\/p>\n<p>        for td in td_elements:<\/p>\n<p>            yield {&#39;text&#39;: td.css(&#39;::text&#39;).get()}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8fd0\u884c\u722c\u866b<\/h4>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u8fd0\u884c\u722c\u866b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">scrapy crawl myspider -o output.json<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u590d\u6742\u7f51\u9875\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u7f51\u9875\u7ed3\u6784\u590d\u6742\uff0c\u7b80\u5355\u7684\u9009\u62e9\u5668\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528BeautifulSoup\u548cSelenium\uff0c\u6216\u8005\u901a\u8fc7XPath\u6765\u7cbe\u786e\u5b9a\u4f4d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>\u7ed3\u5408\u4f7f\u7528BeautifulSoup\u548cSelenium<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>from selenium import webdriver<\/p>\n<h2><strong>\u8bbe\u7f6eWebDriver<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u83b7\u53d6\u9875\u9762\u6e90\u7801<\/strong><\/h2>\n<p>page_source = driver.page_source<\/p>\n<h2><strong>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(page_source, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u6240\u6709&lt;td class=&quot;your-class-name&quot;&gt;<\/strong><\/h2>\n<p>td_elements = soup.find_all(&#39;td&#39;, class_=&#39;your-class-name&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u5185\u5bb9<\/strong><\/h2>\n<p>for td in td_elements:<\/p>\n<p>    print(td.text)<\/p>\n<h2><strong>\u5173\u95edWebDriver<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528XPath<\/h4>\n<\/p>\n<p><p>XPath\u662f\u4e00\u79cd\u7528\u4e8e\u5728XML\u6587\u6863\u4e2d\u67e5\u627e\u4fe1\u606f\u7684\u8bed\u8a00\uff0c\u9002\u7528\u4e8eHTML\u6587\u6863\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u8bbe\u7f6eWebDriver<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u4f7f\u7528XPath\u67e5\u627e\u6240\u6709&lt;td class=&quot;your-class-name&quot;&gt;<\/strong><\/h2>\n<p>td_elements = driver.find_elements_by_xpath(&#39;\/\/td[@class=&quot;your-class-name&quot;]&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u5185\u5bb9<\/strong><\/h2>\n<p>for td in td_elements:<\/p>\n<p>    print(td.text)<\/p>\n<h2><strong>\u5173\u95edWebDriver<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u53cd\u722c\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u8bb8\u591a\u7f51\u7ad9\u90fd\u6709\u53cd\u722c\u673a\u5236\uff0c\u5982IP\u5c01\u7981\u3001\u9a8c\u8bc1\u7801\u3001\u52a8\u6001\u5185\u5bb9\u52a0\u8f7d\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u53cd\u722c\u673a\u5236\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u4ee3\u7406IP<\/h4>\n<\/p>\n<p><p>\u4ee3\u7406IP\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ed5\u8fc7IP\u5c01\u7981\u3002\u53ef\u4ee5\u4f7f\u7528\u514d\u8d39\u7684\u4ee3\u7406IP\u670d\u52a1\u6216\u8d2d\u4e70\u9ad8\u8d28\u91cf\u7684\u4ee3\u7406IP\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>proxies = {<\/p>\n<p>    &#39;http&#39;: &#39;http:\/\/your-proxy-ip:port&#39;,<\/p>\n<p>    &#39;https&#39;: &#39;http:\/\/your-proxy-ip:port&#39;,<\/p>\n<p>}<\/p>\n<p>response = requests.get(&#39;http:\/\/example.com&#39;, proxies=proxies)<\/p>\n<p>print(response.content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5904\u7406\u9a8c\u8bc1\u7801<\/h4>\n<\/p>\n<p><p>\u9a8c\u8bc1\u7801\u901a\u5e38\u9700\u8981\u901a\u8fc7\u56fe\u50cf\u8bc6\u522b\u6216\u624b\u52a8\u89e3\u51b3\u3002\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u9a8c\u8bc1\u7801\u8bc6\u522b\u670d\u52a1\uff0c\u5982\u6253\u7801\u5e73\u53f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from PIL import Image<\/p>\n<p>import pytesseract<\/p>\n<h2><strong>\u8bbe\u7f6eWebDriver<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u622a\u56fe\u9a8c\u8bc1\u7801<\/strong><\/h2>\n<p>captcha_element = driver.find_element_by_id(&#39;captcha&#39;)<\/p>\n<p>captcha_element.screenshot(&#39;captcha.png&#39;)<\/p>\n<h2><strong>\u8bc6\u522b\u9a8c\u8bc1\u7801<\/strong><\/h2>\n<p>captcha_text = pytesseract.image_to_string(Image.open(&#39;captcha.png&#39;))<\/p>\n<p>print(captcha_text)<\/p>\n<h2><strong>\u5173\u95edWebDriver<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u52a8\u6001\u5185\u5bb9\u52a0\u8f7d<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\uff0c\u901a\u5e38\u9700\u8981\u7b49\u5f85\u9875\u9762\u5b8c\u5168\u52a0\u8f7d\u540e\u518d\u8fdb\u884c\u722c\u53d6\u3002\u53ef\u4ee5\u4f7f\u7528Selenium\u7684\u7b49\u5f85\u529f\u80fd\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.support.ui import WebDriverW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/p>\n<p>from selenium.webdriver.support import expected_conditions as EC<\/p>\n<h2><strong>\u8bbe\u7f6eWebDriver<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u7b49\u5f85&lt;td class=&quot;your-class-name&quot;&gt;\u5143\u7d20\u52a0\u8f7d<\/strong><\/h2>\n<p>WebDriverWait(driver, 10).until(<\/p>\n<p>    EC.presence_of_element_located((By.CSS_SELECTOR, &#39;td.your-class-name&#39;))<\/p>\n<p>)<\/p>\n<h2><strong>\u67e5\u627e\u6240\u6709&lt;td class=&quot;your-class-name&quot;&gt;<\/strong><\/h2>\n<p>td_elements = driver.find_elements_by_css_selector(&#39;td.your-class-name&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u5185\u5bb9<\/strong><\/h2>\n<p>for td in td_elements:<\/p>\n<p>    print(td.text)<\/p>\n<h2><strong>\u5173\u95edWebDriver<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6570\u636e\u5b58\u50a8\u4e0e\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u722c\u53d6\u5230\u7684\u6570\u636e\u9700\u8981\u8fdb\u884c\u5b58\u50a8\u4e0e\u7ba1\u7406\uff0c\u53ef\u4ee5\u9009\u62e9\u591a\u79cd\u5b58\u50a8\u65b9\u5f0f\uff0c\u5982CSV\u3001JSON\u3001\u6570\u636e\u5e93\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u5b58\u50a8\u4e3aCSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [{&#39;text&#39;: &#39;example1&#39;}, {&#39;text&#39;: &#39;example2&#39;}]<\/p>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    fieldnames = [&#39;text&#39;]<\/p>\n<p>    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)<\/p>\n<p>    writer.writeheader()<\/p>\n<p>    for row in data:<\/p>\n<p>        writer.writerow(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b58\u50a8\u4e3aJSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = [{&#39;text&#39;: &#39;example1&#39;}, {&#39;text&#39;: &#39;example2&#39;}]<\/p>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as jsonfile:<\/p>\n<p>    json.dump(data, jsonfile)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b58\u50a8\u5230\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4ee5SQLite\u6570\u636e\u5e93\u4e3a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS data (text TEXT)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>data = [{&#39;text&#39;: &#39;example1&#39;}, {&#39;text&#39;: &#39;example2&#39;}]<\/p>\n<p>for row in data:<\/p>\n<p>    c.execute(&quot;INSERT INTO data (text) VALUES (?)&quot;, (row[&#39;text&#39;],))<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u7f51\u9875\u7ed3\u6784\u53d8\u5316<\/h3>\n<\/p>\n<p><p>\u7f51\u9875\u7ed3\u6784\u53ef\u80fd\u4f1a\u968f\u65f6\u53d8\u5316\uff0c\u5bfc\u81f4\u722c\u866b\u5931\u6548\u3002\u4e3a\u4e86\u63d0\u9ad8\u722c\u866b\u7684\u9c81\u68d2\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u66f4\u901a\u7528\u7684\u9009\u62e9\u5668<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5668\u5c3d\u91cf\u4f7f\u7528\u901a\u7528\u7684\u5c5e\u6027\uff0c\u4ee5\u51cf\u5c11\u7f51\u9875\u7ed3\u6784\u53d8\u5316\u5bf9\u722c\u866b\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">td_elements = soup.find_all(&#39;td&#39;, {&#39;class&#39;: lambda x: x and &#39;your-class-name&#39; in x.split()})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b9a\u671f\u7ef4\u62a4\u548c\u66f4\u65b0\u722c\u866b<\/h4>\n<\/p>\n<p><p>\u5b9a\u671f\u68c0\u67e5\u76ee\u6807\u7f51\u9875\u7684\u7ed3\u6784\uff0c\u5e76\u53ca\u65f6\u66f4\u65b0\u722c\u866b\u4ee3\u7801\uff0c\u4ee5\u5e94\u5bf9\u7f51\u9875\u7ed3\u6784\u7684\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6280\u672f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6280\u672f\u6765\u81ea\u52a8\u9002\u5e94\u7f51\u9875\u7ed3\u6784\u7684\u53d8\u5316\uff0c\u4f8b\u5982\u901a\u8fc7\u8bad\u7ec3\u6a21\u578b\u6765\u8bc6\u522b\u7f51\u9875\u4e2d\u7684\u76ee\u6807\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u722c\u866b\u7684\u9053\u5fb7\u4e0e\u6cd5\u5f8b\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7f51\u9875\u722c\u53d6\u65f6\uff0c\u5fc5\u987b\u9075\u5b88\u76ee\u6807\u7f51\u7ad9\u7684robots.txt\u89c4\u5219\uff0c\u5c0a\u91cd\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\u548c\u9690\u79c1\u653f\u7b56\uff0c\u907f\u514d\u5bf9\u7f51\u7ad9\u9020\u6210\u8fc7\u5ea6\u8d1f\u8f7d\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4e0d\u722c\u53d6\u654f\u611f\u4fe1\u606f\u6216\u4fb5\u72af\u4ed6\u4eba\u9690\u79c1\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528BeautifulSoup\u3001Selenium\u548cScrapy\u7b49\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u722c\u53d6\u7f51\u9875\u4e2d<code>&lt;td class=...&gt;<\/code>\u7684\u5185\u5bb9\u3002\u5904\u7406\u590d\u6742\u7f51\u9875\u7ed3\u6784\u3001\u53cd\u722c\u673a\u5236\u548c\u6570\u636e\u5b58\u50a8\u4e0e\u7ba1\u7406\u662f\u722c\u866b\u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u73af\u8282\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5fc5\u987b\u9075\u5b88\u9053\u5fb7\u548c\u6cd5\u5f8b\u89c4\u8303\uff0c\u786e\u4fdd\u722c\u866b\u7684\u5408\u6cd5\u6027\u548c\u5408\u89c4\u6027\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\u722c\u866b\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u722c\u53d6\u7f51\u9875\u4e2d\u7684\u8868\u683c\u6570\u636e\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u722c\u53d6\u7f51\u9875\u4e2d\u7684\u8868\u683c\u6570\u636e\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u89e3\u6790HTML\u3002\u9996\u5148\uff0c\u53d1\u9001\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\uff0c\u7136\u540e\u627e\u5230\u8868\u683c\u7684<code>&lt;td&gt;<\/code>\u6807\u7b7e\uff0c\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528<code>requests.get()<\/code>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002<\/li>\n<li>\u7528<code>BeautifulSoup<\/code>\u89e3\u6790HTML\u3002<\/li>\n<li>\u4f7f\u7528\u9002\u5f53\u7684\u9009\u62e9\u5668\uff08\u5982<code>find_all()<\/code>\uff09\u627e\u5230\u6240\u6709<code>&lt;td&gt;<\/code>\u6807\u7b7e\u3002<\/li>\n<li>\u904d\u5386\u63d0\u53d6\u7684\u6570\u636e\u5e76\u5b58\u50a8\u6216\u5904\u7406\u3002<\/li>\n<\/ol>\n<p><strong>\u5728\u722c\u53d6\u6570\u636e\u65f6\u5982\u4f55\u5904\u7406\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u8bb8\u591a\u7f51\u7ad9\u4f1a\u5b9e\u65bd\u53cd\u722c\u866b\u673a\u5236\u6765\u9650\u5236\u81ea\u52a8\u5316\u8bf7\u6c42\u3002\u4e3a\u907f\u514d\u88ab\u5c01\u7981\uff0c\u53ef\u4ee5\u91c7\u53d6\u4e00\u4e9b\u7b56\u7565\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u968f\u673a\u8bbe\u7f6e\u8bf7\u6c42\u5934\u90e8\uff0c\u6a21\u62df\u771f\u5b9e\u7528\u6237\u7684\u6d4f\u89c8\u5668\u3002<\/li>\n<li>\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694\uff0c\u907f\u514d\u9891\u7e41\u8bf7\u6c42\u540c\u4e00\u9875\u9762\u3002<\/li>\n<li>\u4f7f\u7528\u4ee3\u7406IP\u6765\u5206\u6563\u8bf7\u6c42\u6765\u6e90\u3002<\/li>\n<li>\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u6a21\u62df\u771f\u5b9e\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u5c06\u722c\u53d6\u5230\u7684\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5c06\u722c\u53d6\u5230\u7684\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u76f8\u5bf9\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u6216<code>pandas<\/code>\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2aCSV\u6587\u4ef6\u5e76\u6253\u5f00\u3002<\/li>\n<li>\u4f7f\u7528<code>csv.writer()<\/code>\u6216<code>pandas.DataFrame.to_csv()<\/code>\u65b9\u6cd5\u5199\u5165\u6570\u636e\u3002<\/li>\n<li>\u786e\u4fdd\u5728\u5199\u5165\u6570\u636e\u65f6\u5173\u95ed\u6587\u4ef6\uff0c\u6216\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u5173\u95ed\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u722c\u53d6\u5230\u7684\u8868\u683c\u6570\u636e\u5b58\u50a8\u4e3aCSV\u683c\u5f0f\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u6216\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u722c\u53d6&lt;td class=&#8230;&gt;\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u901a\u8fc7BeautifulSoup [&hellip;]","protected":false},"author":3,"featured_media":1174701,"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\/1174693"}],"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=1174693"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174693\/revisions"}],"predecessor-version":[{"id":1174702,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174693\/revisions\/1174702"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174701"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}