-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
22 lines (18 loc) · 860 Bytes
/
test.py
File metadata and controls
22 lines (18 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import os
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
web = requests.get('https://www.nkust.edu.tw/p/412-1000-2797.php', cookies={'over18': '1'})
soup = BeautifulSoup(web.text, "html.parser")
imgs = soup.find_all('img')
target_name = "高科大散步地圖" # 設定目標圖片的名稱
name = 0
base_url = 'https://www.nkust.edu.tw' # 網站的基本網址
for i in imgs:
if i.get('alt') == target_name: # 使用條件判斷來選擇符合名稱的圖片
img_url = urljoin(base_url, i['src']) # 轉換相對網址為絕對網址
img_data = requests.get(img_url).content # 下載圖片的內容
os.makedirs('img2', exist_ok=True) # 自動建立 img 資料夾
with open(f'img2/test_{name}.jpg', 'wb') as f:
f.write(img_data) # 寫入圖片的內容
name += 1