下载图像时预期的字符串或字节(如对象错误)

时间:2018-08-05 04:14:13

标签: json python-3.x beautifulsoup urllib

我正在尝试通过Google搜索下载图像,但出现错误提示

无法加载:预期的字符串或类似字节的对象。

我经历了类似的答案here,但没有答案。我也尝试过强制转换链接提取到字符串,但仍然是相同的错误。这是我的代码:

from bs4 import BeautifulSoup
import requests
import re
import urllib.request as ulib
import os
import json

def get_soup(url,header):
    return BeautifulSoup(ulib.urlopen(ulib.Request(url,headers=header)),"html.parser")


query = input("fidget spinner")# you can change the query for the image  here
image_type="ActiOn"
query= query.split()
query='+'.join(query)
url="https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch"
#add the directory for your image here
DIR="new_model_data"+query.split('+')[0]+"\\"
header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"}
soup = get_soup(url,header)


ActualImages=[]# contains the link for Large original images, type of  image
for a in soup.find_all("div",{"class":"rg_meta"}):
    link , Type =json.loads(a.text)["ou"]  ,json.loads(a.text)["ity"]
    ActualImages.append((link,Type))

print("there are total" , len(ActualImages),"images")


###print images
for i , (img , Type) in enumerate( ActualImages):
    try:
        req = ulib.Request(img, headers={'User-Agent' : header})
        raw_img = ulib.urlopen(req).read()
        if not os.path.exists(DIR):
            os.mkdir(DIR)
        cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1
        print(cntr)
        if len(Type)==0:
            f = open(DIR + image_type + "_"+ str(cntr)+".jpg", 'wb')
        else :
            f = open(DIR + image_type + "_"+ str(cntr)+"."+Type, 'wb')


        f.write(raw_img)
        f.close()
    except Exception as e:
        print("could not load : "+img)
        print(e)

获取了链接,但下载图像带来了问题。 这是完整的堆栈跟踪:

fidget spinnerfidget spinner
there are total 100 images
could not load : https://upload.wikimedia.org/wikipedia/commons/f/f3/Fidget_spinner_red%2C_cropped.jpg
expected string or bytes-like object
could not load : https://images-na.ssl-images-amazon.com/images/I/61SsKtsobnL._SX425_.jpg
expected string or bytes-like object
could not load : https://images.hobbytron.com/ZX-33127-lg.jpg
expected string or bytes-like object
could not load : https://pisces.bbystatic.com/image2/BestBuy_US/images/products/5897/5897412_sd.jpg;maxHeight=640;maxWidth=550
expected string or bytes-like object
could not load : https://www.fluidbranding.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/f/i/fidget-spinner-blue.jpg
expected string or bytes-like object
could not load : https://www.dpciwholesale.com/images/D/9819.JPG
expected string or bytes-like object
could not load : https://cdn.shopify.com/s/files/1/1132/0554/products/fidget_spinner_grande.gif?v=1508962897
expected string or bytes-like object

请帮助

0 个答案:

没有答案