Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

python webscraping how can i go through a page a download the src image attached to…

  1. User

    python webscraping how can i go through a page a download the src image attached to every div with the class "thumb"

  2. Assistant

    To achieve that, you can use a Python library called Beautiful Soup, which helps to parse HTML and extract information from it. Additionally, you'll need the requests library to handle downloading the images.

    First, make sure you have both libraries installed:

    pip install beautifulsoup4
    pip install requests
    

    Then, here's an example of how to download all images from div tags with the class "thumb":

    import os
    import requests
    from bs4 import BeautifulSoup
    
    # URL of the webpage you want to scrape
    url = "https://example.com"
    
    # Fetch the webpage content
    response = requests.get(url)
    content = response.content
    
    # Parse the content using Beautiful Soup
    soup = BeautifulSoup(content, "html.parser")
    
    # Find all div tags with the class "thumb"
    image_divs = soup.find_all("div", class_="thumb")
    
    # Download each image from the divs
    for index, div in enumerate(image_divs):
        img = div.find("img")
        src = img["src"]
    
        # Download the image and save it to a file
        image_data = requests.get(src).content
    
        # Setup a folder called 'downloaded_images' to store the images
        os.makedirs('downloaded_images', exist_ok=True)
    
        with open(f"downloaded_images/image_{index}.jpg", "wb") as image_file:
            image_file.write(image_data)
    
        print(f"Downloaded image {index} from '{src}'")
    

    Remember to replace "https://example.com" with the actual page you want to scrape and you may also adjust the img["src"] if the image is stored in a different attribute.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.