概要
img タグを取得してその src 属性を取得するだけです
環境
- macOS 14.0
- Python 3.11.6
サンプルコード
import requests
from bs4 import BeautifulSoup
class Downloader:
def __init__(self, url: str = "https://picsum.photos/"):
super().__init__()
self.url = url
def get_img_urls(self) -> list:
response = None
try:
with requests.Session() as session:
response = session.get(self.url, timeout=60)
if response.status_code == 200:
soup = BeautifulSoup(response.text, "html.parser")
img_urls = [element.get("src") for element in soup.find_all("img")]
return img_urls
except Exception as e:
print(e)
if response:
response.close()
return []
if __name__ == "__main__":
dl = Downloader()
for img_url in dl.get_img_urls():
print(img_url)
0 件のコメント:
コメントを投稿