Home > database >  extracting url of an image using BeautifulSoup
extracting url of an image using BeautifulSoup

Time:01-22

I want to extract the url of the pic from the website pepperfry. Not able to do that. This is the html code from which I need my url. I am using python.

<img src="https://ii1.pepperfry.com/supplierpng/1e76ddb7648665e70319edaae34b93ec.png?_v=13" data-src="https://ii1.pepperfry.com/supplierpng/1e76ddb7648665e70319edaae34b93ec.png?_v=13" alt="Merchant Info" >

CodePudding user response:

import requests
r = requests.get("<your site>")
content = BeautifulSoup(r.content, "html.parser")

imgs = content.find_all("img", class_ = "mCS_img_loaded loaded")
for element in imgs:
    element['src'] # the url

CodePudding user response:

You do not confirm the url of the page you're trying to scrape, and you're not posting your code attempts.

Nonetheless, in the hope your next questions will respect the guidelines outlined here, you can find below an example of how you can get that image url:

import requests
from bs4 import BeautifulSoup as bs

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}

url = 'https://www.pepperfry.com/bolton-king-size-bed-with-hydraulic-storage-in-wenge-finish-by-hometown-1638834.html?type=clip&pos=2:1&cat=46&total_result=488&variation_id=213074'

r = requests.get(url, headers=headers)
soup = bs(r.text, 'html.parser')
big_img_url = soup.select_one('div[] img[itemprop="image"]').get('data-src')
print(big_img_url) 

Result in terminal:

https://ii1.pepperfry.com/media/catalog/product/b/o/800x880/bolton-king-size-bed-with-storage-in-wenge-finish-by-hometown-bolton-king-size-bed-with-storage-in-w-6ebo3z.jpg

See BeautifulSoup documentation for more details.

  • Related