파이썬 구글 이미지 크롤링
조회수 3030회
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
1. 저장 자체는, 이미 누가 만들어놓은 구글이미지 크롤링 파이썬 스크립트를 보면, 그냥 이렇게 하면 됩니다.
try: output_file = open(path, 'wb') output_file.write(data) output_file.close() except OSError as e: # OS에 문제가 있을때.. except IOError as e: # 파일 쓰기에 문제가 있을 때...
2. 이게 문제가 아니고... 지금 하고 계신 방식으로는 그 이미지들을 저장하고 싶지 않으실 겁니다.
예컨대 out focus background라고 검색한 결과를 Postman이라는 좋은 프로그램 가지고 모의 "크롤링"을 해보았는데요 결과가 (스샷에서 주신 것처럼) 이런
img
태그들이 떨어집니다.<img height="73" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRTOnuOvOOdhatyAEgGLu5ZWy47sec9K5yU0HCDwGs-775g0Ot3nfgc0A" width="110" alt="out focus background에 대한 이미지 검색결과">
그리고 저 이미지를 열어 보면 이렇게 생겼어요. 가로 150 세로 84입니다.
아마도 페이지 로드 후 js가 실행되어야 이런 형태로 바뀌는 것 같아요. 그러고 나면
a
태그에 들어 있는 링크를 이용해서 "저장할 만한" 이미지를 얻을 수 있을 것 같습니다.<a jsname="hSRGPd" href="/imgres?imgurl=https%3A%2F%2Fthumbs.dreamstime.com%2Ft%2Fgreen-natural-background-out-focus-forest-bokeh-40681278.jpg&imgrefurl=https%3A%2F%2Fwww.dreamstime.com%2Ffresh-green-bio-abstract-blurred-background-spring-summer-abstract-nature-background-green-blurred-background-green-bokeh-out-image122604484&docid=gR2HU0bA_lu_1M&tbnid=HRYs1XvtajYhGM%3A&vet=10ahUKEwiy0NWTkIXfAhVJxrwKHQxNAmMQMwhMKBEwEQ..i&w=240&h=160&client=firefox-b-ab&bih=966&biw=862&q=out%20focus%20background&ved=0ahUKEwiy0NWTkIXfAhVJxrwKHQxNAmMQMwhMKBEwEQ&iact=mrc&uact=8" jsaction="fire.ivg_o;mouseover:str.hmov;mouseout:str.hmou" class="rg_l" rel="noopener" style="width: 233px; height: 192px; left: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%;"> <div class="THL2l"></div> <img class="rg_ic rg_i" data-src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSMsyc_VQeuTv5P47F2uYrBRyQyu09APtKgLiH24N4BKrBvs1Dh" jsaction="load:str.tbn" alt="out focus background에 대한 이미지 검색결과" onload="typeof google==='object'&&google.aft&&google.aft(this)" style="width:240px;height:160px;margin-left:-3px;margin-right:-4px;margin-top:16px" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSMsyc_VQeuTv5P47F2uYrBRyQyu09APtKgLiH24N4BKrBvs1Dh"> <div class="rg_ilmbg" style="bottom:16px;left:0px;max-width:233px"> 240 × 160 </div> </a>
댓글 입력