{"id":931758,"date":"2024-12-26T17:43:56","date_gmt":"2024-12-26T09:43:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/931758.html"},"modified":"2024-12-26T17:43:58","modified_gmt":"2024-12-26T09:43:58","slug":"python%e5%a6%82%e4%bd%95%e8%af%86%e5%88%abhref","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/931758.html","title":{"rendered":"python\u5982\u4f55\u8bc6\u522bhref"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065933\/7ebceab7-d5e2-46cc-a3d2-d7e6627cfde0.webp\" alt=\"python\u5982\u4f55\u8bc6\u522bhref\" \/><\/p>\n<p><p> <strong>Python\u8bc6\u522bhref\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528BeautifulSoup\u5e93\u3001\u4f7f\u7528lxml\u5e93\u3001\u4f7f\u7528Scrapy\u6846\u67b6\u3002<\/strong>\u5728\u8fd9\u51e0\u79cd\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528BeautifulSoup\u5e93<\/strong>\u662f\u6700\u5e38\u89c1\u548c\u7b80\u4fbf\u7684\u65b9\u5f0f\u3002BeautifulSoup\u662f\u4e00\u4e2aPython\u5e93\uff0c\u7528\u4e8e\u4eceHTML\u6216XML\u6587\u4ef6\u4e2d\u63d0\u53d6\u6570\u636e\u3002\u901a\u8fc7\u5b83\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5730\u904d\u5386\u6587\u6863\u6811\u3001\u641c\u7d22\u7279\u5b9a\u7684\u6807\u7b7e\u548c\u5c5e\u6027\u3002\u4f7f\u7528BeautifulSoup\u8bc6\u522bhref\u5c5e\u6027\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5e93\u3001\u52a0\u8f7dHTML\u6587\u6863\u3001\u67e5\u627e\u5e26\u6709href\u5c5e\u6027\u7684\u6807\u7b7e\u5e76\u63d0\u53d6\u5176\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u63d0\u53d6HTML\u6587\u6863\u4e2d\u7684href\u5c5e\u6027\u3002\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165re\u5e93\uff0c\u7136\u540e\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u6765\u5339\u914dhref\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>html = &#39;&#39;&#39;&lt;a href=&quot;https:\/\/example.com&quot;&gt;Example&lt;\/a&gt;&#39;&#39;&#39;<\/p>\n<p>hrefs = re.findall(r&#39;href=&quot;(.*?)&quot;&#39;, html)<\/p>\n<p>for href in hrefs:<\/p>\n<p>    print(href)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7075\u6d3b\u6027\u5141\u8bb8\u6211\u4eec\u5339\u914d\u591a\u79cd\u683c\u5f0f\u7684href\u5c5e\u6027\uff0c\u4f46\u7531\u4e8eHTML\u6587\u6863\u7684\u590d\u6742\u6027\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u4f1a\u6f0f\u6389\u4e00\u4e9b\u8fb9\u7f18\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528BeautifulSoup\u5e93<\/p>\n<\/p>\n<p><p>BeautifulSoup\u662fPython\u4e2d\u5904\u7406HTML\u548cXML\u7684\u5e38\u7528\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u89e3\u6790HTML\u6587\u6863\u5e76\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5BeautifulSoup\u5e93<\/li>\n<\/ol>\n<p><p>\u8981\u4f7f\u7528BeautifulSoup\u5e93\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528BeautifulSoup\u5e93\u8bc6\u522bHTML\u6587\u6863\u4e2d\u7684href\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html = &#39;&#39;&#39;&lt;a href=&quot;https:\/\/example.com&quot;&gt;Example&lt;\/a&gt;&#39;&#39;&#39;<\/p>\n<p>soup = BeautifulSoup(html, &#39;html.parser&#39;)<\/p>\n<p>for a_tag in soup.find_all(&#39;a&#39;, href=True):<\/p>\n<p>    print(a_tag[&#39;href&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2aBeautifulSoup\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528find_all\u65b9\u6cd5\u67e5\u627e\u6240\u6709\u5e26\u6709href\u5c5e\u6027\u7684<a>\u6807\u7b7e\u3002\u6700\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u8bbf\u95ee\u6807\u7b7e\u7684href\u5c5e\u6027\u6765\u63d0\u53d6\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528lxml\u5e93<\/p>\n<\/p>\n<p><p>lxml\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u7684Python\u5e93\u3002\u4e0eBeautifulSoup\u76f8\u6bd4\uff0clxml\u901a\u5e38\u66f4\u5feb\u901f\u548c\u9ad8\u6548\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u578b\u6587\u6863\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5lxml\u5e93<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5lxml\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528lxml\u89e3\u6790HTML<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528lxml\u5e93\u63d0\u53d6HTML\u6587\u6863\u4e2d\u7684href\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from lxml import html<\/p>\n<p>html_content = &#39;&#39;&#39;&lt;a href=&quot;https:\/\/example.com&quot;&gt;Example&lt;\/a&gt;&#39;&#39;&#39;<\/p>\n<p>tree = html.fromstring(html_content)<\/p>\n<p>hrefs = tree.xpath(&#39;\/\/a[@href]\/@href&#39;)<\/p>\n<p>for href in hrefs:<\/p>\n<p>    print(href)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528lxml\u5e93\u7684xpath\u65b9\u6cd5\u6765\u67e5\u627e\u6240\u6709\u5e26\u6709href\u5c5e\u6027\u7684<a>\u6807\u7b7e\uff0c\u5e76\u63d0\u53d6\u5176\u503c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Scrapy\u6846\u67b6<\/p>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u6846\u67b6\uff0c\u7528\u4e8e\u7f51\u7edc\u722c\u866b\u5f00\u53d1\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u6765\u6293\u53d6\u548c\u89e3\u6790\u7f51\u9875\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Scrapy\u6846\u67b6<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Scrapy\u6846\u67b6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scrapy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Scrapy\u63d0\u53d6href<\/li>\n<\/ol>\n<p><p>Scrapy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u94fe\u63a5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\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;my_spider&#39;<\/p>\n<p>    start_urls = [&#39;https:\/\/example.com&#39;]<\/p>\n<p>    def parse(self, response):<\/p>\n<p>        for href in response.css(&#39;a::attr(href)&#39;).extract():<\/p>\n<p>            print(href)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aScrapy Spider\u7c7b\uff0c\u5e76\u5b9e\u73b0\u4e86parse\u65b9\u6cd5\u3002\u901a\u8fc7response\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528CSS\u9009\u62e9\u5668\u6765\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5bf9\u6bd4\u4e0e\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728\u8bc6\u522bhref\u5c5e\u6027\u65f6\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002<strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u9002\u5408\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u5339\u914d\uff0c\u4f46\u5728\u5904\u7406\u590d\u6742HTML\u65f6\u53ef\u80fd\u4f1a\u6709\u5c40\u9650\u6027\u3002<strong>BeautifulSoup<\/strong>\u63d0\u4f9b\u4e86\u7b80\u4fbf\u7684API\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002<strong>lxml<\/strong>\u6027\u80fd\u4f18\u5f02\uff0c\u9002\u5408\u5927\u6570\u636e\u91cf\u5904\u7406\u3002<strong>Scrapy<\/strong>\u5219\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u9700\u8981\u6293\u53d6\u548c\u89e3\u6790\u5927\u91cf\u7f51\u9875\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002\u5982\u679c\u9700\u8981\u5feb\u901f\u63d0\u53d6\u5c0f\u578b\u6587\u6863\u4e2d\u7684\u94fe\u63a5\uff0cBeautifulSoup\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u800c\u5bf9\u4e8e\u5927\u578b\u6587\u6863\u6216\u9700\u8981\u9ad8\u6027\u80fd\u89e3\u6790\u7684\u5e94\u7528\uff0clxml\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\uff1b\u5982\u679c\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u7f51\u9875\u6293\u53d6\uff0cScrapy\u5219\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684\u89e3\u51b3\u65b9\u6848\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u4e86\u89e3\u5176\u5de5\u4f5c\u539f\u7406\u548c\u9002\u7528\u573a\u666f\u5c06\u6709\u52a9\u4e8e\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\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\u4e2d\u7684\u6240\u6709\u94fe\u63a5\uff1f<\/strong><br \/>\u4f7f\u7528Python\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u94fe\u63a5\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u5e93\u5982BeautifulSoup\u548crequests\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u7528BeautifulSoup\u89e3\u6790HTML\uff0c\u6700\u540e\u63d0\u53d6\u6240\u6709\u7684<code>href<\/code>\u5c5e\u6027\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import requests\nfrom bs4 import BeautifulSoup\n\nurl = &#39;http:\/\/example.com&#39;\nresponse = requests.get(url)\nsoup = BeautifulSoup(response.text, &#39;html.parser&#39;)\n\nlinks = [a[&#39;href&#39;] for a in soup.find_all(&#39;a&#39;, href=True)]\nprint(links)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u80fd\u591f\u5f97\u5230\u7f51\u9875\u4e2d\u6240\u6709\u7684\u94fe\u63a5\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u5904\u7406HTML\u548c\u63d0\u53d6\u94fe\u63a5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5904\u7406HTML\u548c\u63d0\u53d6\u94fe\u63a5\u7684\u5e38\u7528\u5e93\u5305\u62ecBeautifulSoup\u3001lxml\u548chtml.parser\u3002BeautifulSoup\u662f\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\u6765\u89e3\u6790HTML\u548cXML\u6587\u6863\u3002lxml\u5219\u4ee5\u5176\u901f\u5ea6\u548c\u6548\u7387\u8457\u79f0\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6587\u6863\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u548c\u590d\u6742\u6027\u6765\u51b3\u5b9a\u3002<\/p>\n<p><strong>\u63d0\u53d6\u94fe\u63a5\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u63d0\u53d6\u94fe\u63a5\u65f6\uff0c\u5e94\u8be5\u5173\u6ce8\u94fe\u63a5\u7684\u5b8c\u6574\u6027\u548c\u6709\u6548\u6027\u3002\u76f8\u5bf9\u94fe\u63a5\u548c\u7edd\u5bf9\u94fe\u63a5\u7684\u533a\u522b\u9700\u8981\u660e\u786e\uff0c\u786e\u4fdd\u5728\u5904\u7406\u76f8\u5bf9\u94fe\u63a5\u65f6\u80fd\u591f\u6b63\u786e\u62fc\u63a5\u6210\u5b8c\u6574\u7684URL\u3002\u6b64\u5916\uff0c\u8003\u8651\u5230\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\uff0c\u5c0a\u91cd\u7f51\u7ad9\u7684\u722c\u866b\u534f\u8bae\uff0c\u907f\u514d\u5bf9\u7f51\u7ad9\u9020\u6210\u8d1f\u62c5\u6216\u8fdd\u89c4\u6293\u53d6\u3002\u8fd8\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25\u6216HTML\u7ed3\u6784\u53d8\u5316\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bc6\u522bhref\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528BeautifulSoup\u5e93\u3001\u4f7f\u7528lxml\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":931760,"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\/931758"}],"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=931758"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931758\/revisions"}],"predecessor-version":[{"id":931762,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931758\/revisions\/931762"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/931760"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=931758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=931758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=931758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}