Skip to content

Commit a31629a

Browse files
authored
파이썬 크롤링 - 이미지파일 다운로드 예제
1 parent 371f179 commit a31629a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

image_download.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import requests
2+
from bs4 import BeautifulSoup as bs
3+
4+
url = "https://s.pstatic.net/static/www/img/uit/sp_main_6f9b98.png"
5+
r = requests.get(url)
6+
f = open("naver_main.png", "wb")
7+
f.write(r.content)
8+
f.close

image_download2.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import requests
2+
from bs4 import BeautifulSoup as bs
3+
import os.path
4+
5+
url = "http://urll.kr"
6+
r = requests.get(url)
7+
html = r.text
8+
soup = bs(html, 'html.parser')
9+
10+
imgs = soup.find_all('img')
11+
for i in imgs:
12+
# base_url = "http://urll.kr"
13+
f_path = i.get("src")
14+
img_url = f_path
15+
16+
r = requests.get(img_url)
17+
f_name = os.path.basename(f_path)
18+
print(f_name)
19+
try:
20+
with open("images/"+f_name, "wb") as f:
21+
f.write(r.content)
22+
except:
23+
pass
24+

0 commit comments

Comments
 (0)