{"id":127,"date":"2023-01-11T12:34:44","date_gmt":"2023-01-11T04:34:44","guid":{"rendered":"https:\/\/9256.muqesjie.top\/?p=127"},"modified":"2023-01-11T12:34:44","modified_gmt":"2023-01-11T04:34:44","slug":"%e7%bd%91%e9%a1%b5%e5%9b%be%e7%89%87url%e7%9a%84%e6%8a%93%e5%8f%96%e5%92%8c%e4%b8%8b%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/www.assin.top\/index.php\/2023\/01\/11\/%e7%bd%91%e9%a1%b5%e5%9b%be%e7%89%87url%e7%9a%84%e6%8a%93%e5%8f%96%e5%92%8c%e4%b8%8b%e8%bd%bd\/","title":{"rendered":"\u7f51\u9875\u56fe\u7247URL\u7684\u6293\u53d6\u548c\u4e0b\u8f7d"},"content":{"rendered":"\n<p><em>\/\/\u524d\u8a00\uff1a\u9996\u5148\u6bcf\u4e2a\u7f51\u7ad9\u56fe\u7247\u7684URL\u90fd\u662f\u5dee\u4e0d\u591a\u7684\uff0c\u4e00\u822c\u90fd\u662fimg\u5143\u7d20\u50a8\u5b58\u7740src\u8def\u5f84\uff0c\u6240\u4ee5\u53ea\u8981\u8bfb\u53d6\u7f51\u9875\u4e2dimg\u4e2d\u7684src\u8def\u5f84\u4e2d\u7684\u94fe\u63a5\u5373\u53ef<\/em>\uff0c<em>\u8fd9\u91cc\u4f7f\u7528py\u6f14\u793a<\/em><\/p>\n\n\n\n<p>\u524d\u63d0\uff1a\u4f7f\u7528\u4e0b\u9762\u4ee3\u7801\u9700\u8981\u5b89\u88c5python\uff0c\u540c\u65f6\u5b89\u88c5\u5bf9\u5e94\u7684\u5e93<\/p>\n\n\n\n<p><strong>1.\u56fe\u7247\u7684url\u7684\u6293\u53d6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nfrom bs4 import BeautifulSoup\nfrom urllib.parse import urljoin\n\nurl = '\u7f51\u7ad9\u5730\u5740\u9875\u9762'\nresponse = requests.get(url)\nhtml = response.text\nsoup = BeautifulSoup(html, 'html.parser')\nimg_tags = soup.find_all('img')\nfor img in img_tags:\n    img_url = urljoin(url, img&#91;'src'])\n    print(img_url)<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u5c06\u7f51\u9875\u4e2d\u7684\u56fe\u7247url\u63d0\u53d6\u5e76\u6253\u5370\u51fa\uff0c\u53ea\u8981\u590d\u5236\u5c31\u53ef\u4ee5\u83b7\u53d6\u7f51\u9875\u6240\u6709\u7684\u56fe\u7247url<\/p>\n\n\n\n<p><strong>2.\u56fe\u7247\u7684\u4e0b\u8f7d\u4fdd\u5b58<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nimport os\nfrom bs4 import BeautifulSoup\nfrom urllib.parse import urljoin\n\nurl = '\u7f51\u9875\u94fe\u63a5'\nresponse = requests.get(url)\nhtml = response.text\nsoup = BeautifulSoup(html, 'html.parser')\nimg_tags = soup.find_all('img')\n\n# \u8bbe\u7f6e\u4fdd\u5b58\u56fe\u7247\u7684\u8def\u5f84\nsave_path = '\u4f60\u7684\u6587\u4ef6\u5939\u4f4d\u7f6e'\n# \u5224\u65ad\u4fdd\u5b58\u8def\u5f84\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5c31\u521b\u5efa\nif not os.path.exists(save_path):\n    os.makedirs(save_path)\n\nfor img in img_tags:\n    # \u4f7f\u7528 urljoin \u51fd\u6570\u5c06\u76f8\u5bf9\u8def\u5f84\u62fc\u63a5\u6210\u7edd\u5bf9\u8def\u5f84\n    img_url = urljoin(url, img&#91;'src'])\n    # \u83b7\u53d6\u56fe\u7247\u6587\u4ef6\u540d\n    img_name = os.path.basename(img_url)\n    # \u4e0b\u8f7d\u56fe\u7247\n    img_data = requests.get(img_url).content\n    # \u4fdd\u5b58\u56fe\u7247\u5230\u672c\u5730\n    with open(f'{save_path}\/{img_name}', 'wb') as f:\n        f.write(img_data)\n    print(f'\u4e0b\u8f7d\u5e76\u4fdd\u5b58\u56fe\u7247: {img_name}')\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c31\u53ef\u4ee5\u5c06\u7f51\u9875\u4e2d\u7684\u56fe\u7247\u4e0b\u8f7d\u4e0b\u6765\u5e76\u4fdd\u5b58\u5230\u4f60\u8bbe\u5b9a\u7684\u6587\u4ef6\u5939\u91cc\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u6ce8\u610f\u4ee5\u4e0a\u4ec5\u9002\u7528\u4e0e\u5c06\u771f\u5b9e\u56fe\u7247\u8def\u5f84\u5199\u5728src\u91cc\u9762\u7684\uff0c\u5982\u679c\u52a0\u4e86\u5176\u4ed6\u7684\u9632\u76d7\u5c31\u65e0\u6cd5\u83b7\u53d6\u5230\u771f\u5b9e\u7684\u56fe\u7247\u5730\u5740<\/p>\n\n\n\n<p><strong>3.\u56fe\u7247\u7684\u6279\u91cf\u83b7\u53d6url\u94fe\u63a5<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nfrom urllib.parse import urlparse\nfrom bs4 import BeautifulSoup\nfrom urllib.parse import urljoin\n\ninFile = input(\"\u8bf7\u8f93\u5165\u8bfb\u53d6\u7684\u6587\u4ef6\u8def\u5f84\")\noutFile = input(\"\u8bf7\u8f93\u5165\u5199\u5165\u7684\u6587\u4ef6\u8def\u5f84\")\nwith open(inFile, \"r\") as fIn:\n    with open(outFile, \"w\") as fOut:\n        urls = fIn.readlines()\n        for url in urls:\n            uri = urlparse(url)\n            resp = requests.get(url)\n            soup = BeautifulSoup(resp.text, 'html.parser')\n            img_tags = soup.find_all('img')\n            for img in img_tags:\n                img_url = urljoin(f\"{uri.scheme}:\/\/{uri.netloc}\/\", img&#91;'src'])\n                fOut.write(img_url + \"\\n\")<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u56fe\u7247\u5206\u4eab<\/p>\n<\/blockquote>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/muqesjie.top\/wp-content\/uploads\/2023\/01\/87.jpg\" alt=\"\" class=\"wp-image-131\" width=\"816\" height=\"1193\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\/\/\u524d\u8a00\uff1a\u9996\u5148\u6bcf\u4e2a\u7f51\u7ad9\u56fe\u7247\u7684URL\u90fd\u662f\u5dee\u4e0d\u591a\u7684\uff0c\u4e00\u822c\u90fd\u662fimg\u5143\u7d20\u50a8\u5b58\u7740src\u8def\u5f84\uff0c\u6240\u4ee5\u53ea\u8981\u8bfb\u53d6\u7f51\u9875\u4e2dimg\u4e2d\u7684src\u8def\u5f84\u4e2d\u7684\u94fe\u63a5 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"https:\/\/www.assin.top\/index.php\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.assin.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.assin.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.assin.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.assin.top\/index.php\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":0,"href":"https:\/\/www.assin.top\/index.php\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.assin.top\/index.php\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.assin.top\/index.php\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.assin.top\/index.php\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}