I want you to generate python code capable of scraping website for pictures while outputting them into windows download folder. Also, put all the images into new folder named after the page being scraped
Assistant
Sure, here's a Python code snippet using the requests and BeautifulSoup libraries to scrape images from a website and download them into a new folder named after the page being scraped:
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import os
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
# URL of the website to scrape
url = 'https://example.com'
# Send a GET request to the URL
response = requests.get(url)
# Parse the HTML content of the page
soup = BeautifulSoup(response.content, 'html.parser')
# Create a new folder for the images
folder_name = 'images_from_' + url.split('//')[1].replace('/', '_')
os.makedirs(folder_name, exist_ok=True)
# Find all image tags
img_tags = soup.find_all('img')
# Download and save the images
for img_tag in img_tags:
img_url = urljoin(url, img_tag['src'])
img_response = requests.get(img_url)
img_name = os.path.basename(img_url)
with open(os.path.join(folder_name, img_name), 'wb') as f:
f.write(img_response.content)
print('Images downloaded to folder:', folder_name)
Replace 'https://example.com' with the URL of the website you want to scrape. When you run this code, it will create a new folder with the name images_from_example.com (or the appropriate domain name) and download all the images from the webpage into that folder.
User
Create the new folder in windows default download folder
Assistant
Sure! You can use the os.path.join function to create the new folder in the default Windows download folder. Here's the updated code:
python
import os
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
from winreg import *
# Get the default download folder from the Windows registry
key = OpenKey(HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders")
download_folder = QueryValueEx(key, '{374DE290-123F-4565-9164-39C4925E467B}')[0]
CloseKey(key)
# URL of the website to scrape
url = 'https://example.com'
# Send a GET request to the URL
response = requests.get(url)
# Parse the HTML content of the page
soup = BeautifulSoup(response.content, 'html.parser')
# Create a new folder for the images in the default download folder
folder_name = os.path.join(download_folder, 'images_from_' + url.split('//')[1].replace('/', '_'))
os.makedirs(folder_name, exist_ok=True)
# Find all image tags
img_tags = soup.find_all('img')
# Download and save the images
for img_tag in img_tags:
img_url = urljoin(url, img_tag['src'])
img_response = requests.get(img_url)
img_name = os.path.basename(img_url)
with open(os.path.join(folder_name, img_name), 'wb') as f:
f.write(img_response.content)
print('Images downloaded to folder:', folder_name)
In this code, download_folder is obtained from the Windows registry using the winreg module, and the new folder is created inside the default download folder using os.path.join(download_folder, 'images_from_' + url.split('//')[1].replace('/', '_')). When you run this code, it will create a new folder in the default Windows download folder and download all the images from the webpage into that folder.