{"id":1064739,"date":"2024-12-31T16:11:43","date_gmt":"2024-12-31T08:11:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064739.html"},"modified":"2024-12-31T16:11:45","modified_gmt":"2024-12-31T08:11:45","slug":"python%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87div%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1064739.html","title":{"rendered":"python\u5982\u4f55\u901a\u8fc7div\u83b7\u53d6\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/1dface4a-1722-4501-8f53-156191e4523c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u901a\u8fc7div\u83b7\u53d6\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7Python\u83b7\u53d6\u6570\u636e\u5e93\u7684div\u6570\u636e\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c<strong>\u4e3b\u8981\u5305\u62ec\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u3001\u4f7f\u7528Selenium\u8fdb\u884c\u52a8\u6001\u9875\u9762\u6293\u53d6\u3001\u4f7f\u7528Scrapy\u8fdb\u884c\u722c\u866b\u64cd\u4f5c<\/strong>\u3002\u672c\u6587\u5c06\u7740\u91cd\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u901a\u8fc7Selenium\u8fdb\u884c\u52a8\u6001\u9875\u9762\u6293\u53d6\uff0c\u5e76\u7ed3\u5408BeautifulSoup\u8fdb\u884c\u6570\u636e\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u6570\u636e\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u7f51\u9875\u6293\u53d6\u3002\u5bf9\u4e8e\u9700\u8981\u4ece\u7f51\u9875\u7684div\u6807\u7b7e\u4e2d\u83b7\u53d6\u6570\u636e\u5e76\u5b58\u50a8\u5230\u6570\u636e\u5e93\u7684\u4efb\u52a1\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7Selenium\u548cBeautifulSoup\u6293\u53d6\u7f51\u9875\u6570\u636e\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230MySQL\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u8bbe\u7f6e\u73af\u5883<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5fc5\u8981\u7684Python\u5e93\uff1aSelenium\u3001BeautifulSoup\u3001pymysql\u4ee5\u53ca\u6d4f\u89c8\u5668\u9a71\u52a8\u7a0b\u5e8f\uff08\u5982ChromeDriver\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p>pip install beautifulsoup4<\/p>\n<p>pip install pymysql<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528Selenium\u83b7\u53d6\u7f51\u9875\u6570\u636e<\/h2>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u6a21\u62df\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u5904\u7406\u9700\u8981\u52a8\u6001\u52a0\u8f7d\u7684\u7f51\u9875\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u521d\u59cb\u5316Selenium<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521d\u59cb\u5316Selenium\u5e76\u6253\u5f00\u76ee\u6807\u7f51\u9875\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<h2><strong>\u521b\u5efaChrome\u6d4f\u89c8\u5668\u5b9e\u4f8b<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;\/path\/to\/chromedriver&#39;)<\/p>\n<h2><strong>\u6253\u5f00\u76ee\u6807\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;http:\/\/example.com&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u83b7\u53d6div\u6807\u7b7e\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Selenium\u7684find_elements_by_*\u65b9\u6cd5\u6765\u83b7\u53d6\u7f51\u9875\u4e2d\u7684div\u6807\u7b7e\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u6240\u6709\u7684div\u6807\u7b7e<\/p>\n<p>div_elements = driver.find_elements(By.TAG_NAME, &#39;div&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u5e76\u6253\u5370\u6bcf\u4e2adiv\u7684\u6587\u672c\u5185\u5bb9<\/strong><\/h2>\n<p>for div in div_elements:<\/p>\n<p>    print(div.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/h2>\n<\/p>\n<p><p>\u867d\u7136Selenium\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u7f51\u9875\u5143\u7d20\uff0c\u4f46\u4f7f\u7528BeautifulSoup\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u89e3\u6790\u548c\u5904\u7406HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u83b7\u53d6\u7f51\u9875\u6e90\u7801<\/h3>\n<\/p>\n<p><p>\u4eceSelenium\u83b7\u53d6\u7f51\u9875\u6e90\u7801\uff0c\u5e76\u4f7f\u7528BeautifulSoup\u8fdb\u884c\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u6e90\u7801<\/strong><\/h2>\n<p>html_source = driver.page_source<\/p>\n<h2><strong>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_source, &#39;html.parser&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u67e5\u627e\u7279\u5b9a\u7684div\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u7684find_all\u65b9\u6cd5\u67e5\u627e\u7279\u5b9a\u7684div\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u627e\u6240\u6709class\u4e3aexample\u7684div\u6807\u7b7e<\/p>\n<p>divs = soup.find_all(&#39;div&#39;, class_=&#39;example&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u5e76\u6253\u5370\u6bcf\u4e2adiv\u7684\u6587\u672c\u5185\u5bb9<\/strong><\/h2>\n<p>for div in divs:<\/p>\n<p>    print(div.get_text())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93<\/h2>\n<\/p>\n<p><h3>1\u3001\u8fde\u63a5MySQL\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pymysql\u5e93\u8fde\u63a5MySQL\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pymysql<\/p>\n<h2><strong>\u8fde\u63a5\u5230MySQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>connection = pymysql.connect(<\/p>\n<p>    host=&#39;localhost&#39;,<\/p>\n<p>    user=&#39;yourusername&#39;,<\/p>\n<p>    password=&#39;yourpassword&#39;,<\/p>\n<p>    db=&#39;yourdatabase&#39;<\/p>\n<p>)<\/p>\n<p>cursor = connection.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u521b\u5efa\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5c1a\u672a\u521b\u5efa\u8868\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528SQL\u8bed\u53e5\u521b\u5efa\u4e00\u4e2a\u65b0\u8868\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">create_table_query = &#39;&#39;&#39;<\/p>\n<p>CREATE TABLE IF NOT EXISTS div_data (<\/p>\n<p>    id INT AUTO_INCREMENT PRIMARY KEY,<\/p>\n<p>    content TEXT NOT NULL<\/p>\n<p>)<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>cursor.execute(create_table_query)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u63d2\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5c06\u63d0\u53d6\u5230\u7684div\u5185\u5bb9\u63d2\u5165\u5230\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d2\u5165div\u5185\u5bb9\u5230\u6570\u636e\u5e93<\/p>\n<p>for div in divs:<\/p>\n<p>    content = div.get_text()<\/p>\n<p>    insert_query = &#39;INSERT INTO div_data (content) VALUES (%s)&#39;<\/p>\n<p>    cursor.execute(insert_query, (content,))<\/p>\n<h2><strong>\u63d0\u4ea4\u4e8b\u52a1<\/strong><\/h2>\n<p>connection.commit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u5173\u95ed\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u548c\u6d4f\u89c8\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>cursor.close()<\/p>\n<p>connection.close()<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7Python\u7684Selenium\u548cBeautifulSoup\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u7f51\u9875\u4e2d\u7684div\u6807\u7b7e\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u8bbe\u7f6e\u73af\u5883\u3001\u4f7f\u7528Selenium\u83b7\u53d6\u7f51\u9875\u6570\u636e\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u3001\u8fde\u63a5MySQL\u6570\u636e\u5e93\u5e76\u5b58\u50a8\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5bf9\u4ee3\u7801\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528div\u5143\u7d20\u83b7\u53d6\u6570\u636e\u5e93\u4fe1\u606f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528\u7f51\u9875\u722c\u866b\u5e93\uff08\u5982BeautifulSoup\uff09\u6765\u89e3\u6790HTML\u6587\u6863\u4e2d\u7684div\u5143\u7d20\u3002\u901a\u8fc7\u63d0\u53d6div\u4e2d\u7684\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u5b58\u50a8\u8fd9\u4e9b\u4fe1\u606f\u5230\u6570\u636e\u5e93\u4e2d\u3002\u4f60\u9700\u8981\u9996\u5148\u5b89\u88c5BeautifulSoup\u548crequests\u5e93\uff0c\u7136\u540e\u7f16\u5199\u4ee3\u7801\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u89e3\u6790div\uff0c\u5e76\u5c06\u6240\u9700\u6570\u636e\u63d2\u5165\u6570\u636e\u5e93\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u6570\u636e\u5e93\u6765\u5b58\u50a8\u4ecediv\u4e2d\u83b7\u53d6\u7684\u6570\u636e\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u5e93\uff0c\u5982SQLite\u3001PostgreSQL\u3001MySQL\u7b49\u3002\u6839\u636e\u9879\u76ee\u7684\u89c4\u6a21\u548c\u9700\u6c42\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u7684\u6570\u636e\u5e93\u3002SQLite\u9002\u5408\u5c0f\u578b\u9879\u76ee\uff0cMySQL\u548cPostgreSQL\u5219\u9002\u5408\u5927\u578b\u5e94\u7528\u3002\u4f7f\u7528SQLAlchemy\u7b49ORM\u5de5\u5177\u53ef\u4ee5\u7b80\u5316\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u4ecediv\u4e2d\u63d0\u53d6\u7684\u6570\u636e\u662f\u6700\u65b0\u7684\uff1f<\/strong><br \/>\u8981\u786e\u4fdd\u83b7\u53d6\u7684\u6570\u636e\u662f\u6700\u65b0\u7684\uff0c\u53ef\u4ee5\u5b9a\u671f\u8fd0\u884c\u722c\u866b\u811a\u672c\uff0c\u6216\u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1\uff08\u5982cron\u4f5c\u4e1a\uff09\u6765\u81ea\u52a8\u5316\u6570\u636e\u63d0\u53d6\u8fc7\u7a0b\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u68c0\u67e5\u7f51\u9875\u7684\u66f4\u65b0\u65f6\u95f4\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u83b7\u53d6\u76f8\u540c\u7684\u6570\u636e\u3002\u8bbe\u7f6e\u5408\u9002\u7684\u65f6\u95f4\u95f4\u9694\u4e5f\u6709\u52a9\u4e8e\u51cf\u5c11\u8bf7\u6c42\u9891\u7387\uff0c\u907f\u514d\u88ab\u76ee\u6807\u7f51\u7ad9\u5c01\u7981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7Python\u83b7\u53d6\u6570\u636e\u5e93\u7684div\u6570\u636e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u3001\u4f7f\u7528S [&hellip;]","protected":false},"author":3,"featured_media":1064749,"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\/1064739"}],"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=1064739"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064739\/revisions"}],"predecessor-version":[{"id":1064751,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064739\/revisions\/1064751"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1064749"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1064739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1064739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1064739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}