skip to Main Content

Im trying to scrape the link of image from url https://www.eaton.com/us/en-us/skuPage.101012%2520G.html

All my solutions failed; here are my attempts:

  1. print(soup.select_one('[class="module-media-gallery__image lazyload"]')["src"])
    
  2. img=soup.find('img',attrs={'class':'module-media-gallery__image lazyload'})
    
  3. img=soup.find('img',class_='module-media-gallery__image lazyload')
    

3

Answers


  1. I checked website and you have a typo error,
    Image element has class with module-media-gallery__image lazyloaded not module-media-gallery__image lazyload

    <img class="module-media-gallery__image lazyloaded" alt="101012 G - Wireway systems and accessories" data-src="https://www.eaton.com/mdmfiles/PDM726777/4424G-C_R/500x500_72dpi" data-zoom-url="https://www.eaton.com/mdmfiles/PDM726777/4424G-C_R/1000x1000_300dpi"
      src="https://www.eaton.com/mdmfiles/PDM726777/4424G-C_R/500x500_72dpi" style="opacity: 1;">
    Login or Signup to reply.
  2. The image is lazily loaded; the src is not initially set. You can read the value from data-src instead.

    print(soup.select_one('.module-media-gallery__image.lazyload')['data-src'])
    
    Login or Signup to reply.
  3. You can use such a solution using the get() method:

    import requests
    from bs4 import BeautifulSoup
    
    headers = {
            'accept': '*/*',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 YaBrowser/23.1.5.708 Yowser/2.5 Safari/537.36'
        }
    link = "https://www.eaton.com/us/en-us/skuPage.101012%2520G.html"
    
    
    session = requests.Session()
    session.headers.update(headers)
    response = session.get(link)
    src = response.text
    soup = BeautifulSoup(src, 'lxml')
    img=soup.find('img', 'module-media-gallery__image lazyload')
    print(img.get('data-lazy'))
    print(img.get('data-src'))
    print(img.get('data-zoom-url'))
    

    Result:

    https://www.eaton.com/mdmfiles/PDM726777/4424G-C_R/500x500_72dpi
    https://www.eaton.com/mdmfiles/PDM726777/4424G-C_R/500x500_72dpi
    https://www.eaton.com/mdmfiles/PDM726777/4424G-C_R/1000x1000_300dpi
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search