Is there a way I can Download images from any search engine with a code like this?
·
Answer a question
I tried downloading images from bing to a directory but due to some reason the code just executes and gives me nothing.. not even an error.. I used the user-agent HTTP as well.. but it still doesnt seem to be working.. What should i do?
from bs4 import BeautifulSoup
import requests
from PIL import Image
from io import BytesIO
url = 'https://www.bing.com/search'
search = input("Search for: ")
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101
Firefox/80.0'}
params = {"q": search}
r = requests.get(url, headers=headers, params=params)
soup = BeautifulSoup(r.text, "html.parser")
links = soup.findAll("a", {"class": "thumb"})
for item in links:
img_obj = requests.get(item.attrs["href"])
print("Getting", item.attrs["href"])
title = item.attrs["href"].split("/")[-1]
img = Image.open(BytesIO(img_obj.content))
img.save("./scraped_images/" + title, img.format)
Answers
To get all images, you need to add /images to the link. Here's an example with modifications to your code:
from bs4 import BeautifulSoup
from PIL import Image
from io import BytesIO
import requests
import json
search = input("Search for: ")
url = "https://www.bing.com/images/search"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0"
}
params = {"q": search, "form": "HDRSC2", "first": "1", "scenario": "ImageBasicHover"}
r = requests.get(url, headers=headers, params=params)
soup = BeautifulSoup(r.text, "html.parser")
links = soup.find_all("div", {"class": "img_cont hoff"})
for data in soup.find_all("a", {"class": "iusc"}):
json_data = json.loads(data["m"])
img_link = json_data["murl"]
img_object = requests.get(img_link, headers=headers)
title = img_link.split("/")[-1]
print("Getting: ", img_link)
print("Title: ", title + "\n")
img = Image.open(BytesIO(img_object.content))
img.save("./scraped_images/" + title)
更多推荐

所有评论(0)