Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI 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"

    this is an example of the div

  2. Assistant

    You can use the BeautifulSoup library in Python to extract the source images from the divs with class "thumb". Here's the code:

    import requests
    from bs4 import BeautifulSoup
    
    page_url = 'https://example.com'  # replace with your page URL
    response = requests.get(page_url)
    soup = BeautifulSoup(response.text, 'html.parser')
    
    for div in soup.find_all('div', {'class': 'thumb'}):
        img_src = div.img['src']
        print(img_src)
        
        # download the image
        img_data = requests.get(img_src).content
        with open('image.jpg', 'wb') as img_file:
            img_file.write(img_data)
    

    This code first sends a GET request to the URL of the page you want to scrape. Then it creates a BeautifulSoup object to parse the HTML content of the page.

    The for loop finds all the divs with class "thumb" and extracts the src attribute of the img tag inside each div. It then prints the img_src for verification purposes.

    Finally, the code downloads each image using the requests library and saves it as "image.jpg". You may want to modify this code to save the images as different filenames, depending on the actual filenames of the images.

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