본문 바로가기

Python_Crawling/Crawling

[패스트캠퍼스]Selenium 사용 실습 <수정코드>

파이썬 웹 개발

https://www.fastcampus.co.kr/dev_online_pyweb

 

파이썬 웹 개발 올인원 패키지 Online. | 패스트캠퍼스

배우기도 쉬운데, 실무 활용도까지 높은 파이썬, 기본문법부터 실무 예제까지! 온라인 강의로 시작하세요!

www.fastcampus.co.kr

 

기존 코드>

...
print(v.select('a.thumb_link > img')[0]['data-original'])
...

 

기존 코드 오류>

Traceback (most recent call last):
  File "d:\Code\Study\Fastcampus_Webcrawling_Basic\section06-3-c.py", line 97, in <module>
    print(v.select('a.thumb_link > img')[0]['data-original'])
  File "d:\Code\Study\Fastcampus_Webcrawling_Basic\venv\lib\site-packages\bs4\element.py", line 1401, in __getitem__
    return self.attrs[key]
KeyError: 'data-original'

선생님과 다르게 키값을 가져오지 못하는 에러가 발생이 된다.

 

1. 코드를 나눠 출력 하여서 키가 있는지를 확인

...
print(v.select('a.thumb_link > img')[0]
...

 

<img alt="APPLE 2019 맥북프로16 MVVK2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." src="http://img.danawa.com/prod_img/500000/439/911/img/9911439_1.jpg?shrink=160:160&amp;_v=20200319131535"/>

<img alt="APPLE 2020 맥북프로13 MXK32KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." src="http://img.danawa.com/prod_img/500000/225/217/img/11217225_1.jpg?shrink=160:160&amp;_v=20200716140310"/>

<img alt="APPLE 2019 맥북프로16 MVVL2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." src="http://img.danawa.com/prod_img/500000/160/911/img/9911160_1.jpg?shrink=160:160&amp;_v=20200319131535"/>

<img alt="APPLE 2019 맥북프로16 MVVJ2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." src="http://img.danawa.com/prod_img/500000/355/911/img/9911355_1.jpg?shrink=160:160&amp;_v=20200319131535"/>

<img alt="APPLE 2020 맥북프로13 MWP42KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/507/217/img/11217507_1.jpg?shrink=160:160&amp;_v=20200716140430" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2020 맥북에어 MVH22KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/123/872/img/10872123_1.jpg?shrink=160:160&amp;_v=20200507141925" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북프로16 MVVJ2KH/A CTO이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/940/941/img/9941940_1.jpg?shrink=160:160&amp;_v=20200702161743" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2020 맥북에어 MWTJ2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/238/871/img/10871238_1.jpg?shrink=160:160&amp;_v=20200423124839" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북프로16 MVVM2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/418/911/img/9911418_1.jpg?shrink=160:160&amp;_v=20200319131535" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북프로13 MV992KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/100/106/img/8106100_1.jpg?shrink=160:160&amp;_v=20200319131535" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>


<img alt="APPLE 2020 맥북프로13 MWP52KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/570/217/img/11217570_1.jpg?shrink=160:160&amp;_v=20200716140510" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2020 맥북에어 MVH52KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/507/872/img/10872507_1.jpg?shrink=160:160&amp;_v=20200325105411" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2020 맥북에어 MWTL2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/259/871/img/10871259_1.jpg?shrink=160:160&amp;_v=20200618100747" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2018 맥북에어 MREF2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/593/698/img/6698593_1.jpg?shrink=160:160&amp;_v=20200319131125" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2020 맥북프로13 MWP72KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/534/217/img/11217534_1.jpg?shrink=160:160&amp;_v=20200716140204" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2018 맥북프로13 MR9V2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/093/365/img/6365093_1.jpg?shrink=160:160&amp;_v=20200319131017" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2020 맥북에어 MWTK2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/274/871/img/10871274_1.jpg?shrink=160:160&amp;_v=20200325105217" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북프로16 MVVK2KH/A CTO이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/964/941/img/9941964_1.jpg?shrink=160:160&amp;_v=20200319131534" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2020 맥북에어 MVH42KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/519/872/img/10872519_1.jpg?shrink=160:160&amp;_v=20200402135746" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북프로13 MUHN2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/734/919/img/8919734_1.jpg?shrink=160:160&amp;_v=20200319131535" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>


<img alt="APPLE 2017 맥북프로13 MPXT2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/944/250/img/5250944_1.jpg?shrink=160:160&amp;_v=20200319130752" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북프로13 MV962KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/037/106/img/8106037_1.jpg?shrink=160:160&amp;_v=20200319131535" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북프로13 MV972KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/343/106/img/8106343_1.jpg?shrink=160:160&amp;_v=20200319131535" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2020 맥북에어 MVH22KH/A CTO이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/726/914/img/10914726_1.jpg?shrink=160:160&amp;_v=20200331160157" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북프로13 MV9A2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/388/106/img/8106388_1.jpg?shrink=160:160&amp;_v=20200319131535" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북프로13 MUHP2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/342/921/img/8921342_1.jpg?shrink=160:160&amp;_v=20200319131535" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2020 맥북프로13 MWP72KH/A CTO이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/075/448/img/11448075_1.jpg?shrink=160:160&amp;_v=20200608125336" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북에어 MVFL2KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/690/918/img/8918690_1.jpg?shrink=160:160&amp;_v=20200319131210" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2019 맥북프로15 MV932KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/235/100/img/8100235_1.jpg?shrink=160:160&amp;_v=20200319131535" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

<img alt="APPLE 2020 맥북프로13 MXK62KH/A이미지입니다. 누르면 해당 게시물로 새창이동합니다." class="image_lazy" data-original="http://img.danawa.com/prod_img/500000/336/217/img/11217336_1.jpg?shrink=160:160&amp;_v=20200716140054" src="//img.danawa.com/new/noData/img/noImg_130.gif"/>

 

2. 가져와야 될 키 data-original 확인

 

3. 하지만 선생님과 동일하게 코드 사용시 에러 발생

 

4. 해당 코드는 아래의 코드로 변경 후 정상적으로 가능

...
print(v.select("a.thumb_link > img")[0].get("data-original"))
...

 

5. .get를 사용하여서 키를 줘서 해당 값을 가져오게 변경하였음.

 

결과물>

None

None

None

None

http://img.danawa.com/prod_img/500000/507/217/img/11217507_1.jpg?shrink=160:160&_v=20200716140430

http://img.danawa.com/prod_img/500000/123/872/img/10872123_1.jpg?shrink=160:160&_v=20200507141925

http://img.danawa.com/prod_img/500000/940/941/img/9941940_1.jpg?shrink=160:160&_v=20200702161743

http://img.danawa.com/prod_img/500000/238/871/img/10871238_1.jpg?shrink=160:160&_v=20200423124839

http://img.danawa.com/prod_img/500000/418/911/img/9911418_1.jpg?shrink=160:160&_v=20200319131535

http://img.danawa.com/prod_img/500000/100/106/img/8106100_1.jpg?shrink=160:160&_v=20200319131535


http://img.danawa.com/prod_img/500000/570/217/img/11217570_1.jpg?shrink=160:160&_v=20200716140510

http://img.danawa.com/prod_img/500000/507/872/img/10872507_1.jpg?shrink=160:160&_v=20200325105411

http://img.danawa.com/prod_img/500000/259/871/img/10871259_1.jpg?shrink=160:160&_v=20200618100747

http://img.danawa.com/prod_img/500000/593/698/img/6698593_1.jpg?shrink=160:160&_v=20200319131125

http://img.danawa.com/prod_img/500000/534/217/img/11217534_1.jpg?shrink=160:160&_v=20200716140204

http://img.danawa.com/prod_img/500000/093/365/img/6365093_1.jpg?shrink=160:160&_v=20200319131017

http://img.danawa.com/prod_img/500000/274/871/img/10871274_1.jpg?shrink=160:160&_v=20200325105217

http://img.danawa.com/prod_img/500000/964/941/img/9941964_1.jpg?shrink=160:160&_v=20200319131534

http://img.danawa.com/prod_img/500000/519/872/img/10872519_1.jpg?shrink=160:160&_v=20200402135746

http://img.danawa.com/prod_img/500000/734/919/img/8919734_1.jpg?shrink=160:160&_v=20200319131535


http://img.danawa.com/prod_img/500000/944/250/img/5250944_1.jpg?shrink=160:160&_v=20200319130752

http://img.danawa.com/prod_img/500000/037/106/img/8106037_1.jpg?shrink=160:160&_v=20200319131535

http://img.danawa.com/prod_img/500000/343/106/img/8106343_1.jpg?shrink=160:160&_v=20200319131535

http://img.danawa.com/prod_img/500000/726/914/img/10914726_1.jpg?shrink=160:160&_v=20200331160157

http://img.danawa.com/prod_img/500000/388/106/img/8106388_1.jpg?shrink=160:160&_v=20200319131535

http://img.danawa.com/prod_img/500000/342/921/img/8921342_1.jpg?shrink=160:160&_v=20200319131535

http://img.danawa.com/prod_img/500000/075/448/img/11448075_1.jpg?shrink=160:160&_v=20200608125336

http://img.danawa.com/prod_img/500000/690/918/img/8918690_1.jpg?shrink=160:160&_v=20200319131210

http://img.danawa.com/prod_img/500000/235/100/img/8100235_1.jpg?shrink=160:160&_v=20200319131535

http://img.danawa.com/prod_img/500000/336/217/img/11217336_1.jpg?shrink=160:160&_v=20200716140054