Home > Back-end >  Python BeautifulSoup getting KeyError for image src
Python BeautifulSoup getting KeyError for image src

Time:09-29

I am trying to get product image url from this page . I am getting image src but aslo getting this error KeyError: 'data-lazy-src'

here is my code:

image = soup.select('img.attachment-shop_single')

for i in image:
    img = i['data-lazy-src']
    print(img)

python shell result:

https://thedankvape.com/wp-content/uploads/2020/04/ACE-OF-SPADES.jpeg
Traceback (most recent call last):
  File "<pyshell#56>", line 2, in <module>
    img = i['data-lazy-src']
  File "C:\Users\Mick\AppData\Local\Programs\Python\Python39\lib\site-packages\bs4\element.py", line 1406, in __getitem__
    return self.attrs[key]
KeyError: 'data-lazy-src'

I am getting image src but why I am getting key error? here is HTML element

>>>soup.select('img.attachment-shop_single')
[<img alt="ace of spades dank vapes" class="attachment-shop_single size-shop_single wp-post-image" data-lazy-src="https://thedankvape.com/wp-content/uploads/2020/04/ACE-OF-SPADES.jpeg" height="800" src="data:image/svg xml," title="ace of spades dank vapes" width="600"/>, <img alt="ace of spades dank vapes" class="attachment-shop_single size-shop_single wp-post-image" height="800" src="https://thedankvape.com/wp-content/uploads/2020/04/ACE-OF-SPADES.jpeg" title="ace of spades dank vapes" width="600"/>]

how to grab last src element image link src="https://thedankvape.com/wp-content/uploads/2020/04/ACE-OF-SPADES.jpeg?

CodePudding user response:

As stated in the comments, there are two <img> with class "attachment-shop_single". The safest way is to select just one with data-lazy-src attribute:

import requests
from bs4 import BeautifulSoup


url = "https://thedankvape.com/product/ace-of-spades-dank-vapes/"
soup = BeautifulSoup(requests.get(url).content, "html.parser")

img = soup.select_one("[data-lazy-src]")
print(img["data-lazy-src"])

Prints:

https://thedankvape.com/wp-content/uploads/2020/04/VAPE.png
  • Related