【发布时间】:2019-07-24 21:09:04
【问题描述】:
我正在尝试使用以下代码下载图片,但出现错误
from bs4 import BeautifulSoup
import requests
import re
import urllib
import urllib.request as ur
import os
import http.cookiejar as cookielib
import json
def get_soup(url,header):
return BeautifulSoup(ur.urlopen(ur.Request(url,headers=header)),'html.parser')
query = 'apple' #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"
print (url)
#add the directory for your image here
DIR="/Users/jashuvadoma/Desktop/hacking/images"
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")
if not os.path.exists(DIR):
os.mkdir(DIR) DIR = os.path.join(DIR, query.split()[0])
if not os.path.exists(DIR):
os.mkdir(DIR)
###print images
for i , (img , Type) in enumerate( ActualImages):
try:
req = ur.Request(img, headers={'User-Agent' : header})
raw_img = ur.urlopen(req).read()
cntr = lea([i for i in os.listdir(DIR) if image_type in i]) + 1
print (cntr)
if len(Type)==0:
f = open(os.path.join(DIR , image_type + "_"+ str(cntr)+".jpg"), 'wb')
else :
f = open(os.path.join(DIR , image_type + "_"+ str(cntr)+"."+Type), 'wb')
f.write(raw_img)
f.close()
except Exception as e:
print ("could not load : "+img)
print (e)
错误如下: https://www.google.co.in/search?q=apple&source=lnms&tbm=isch 共有 100 张图片 无法加载:https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?201606271147 预期的字符串或类似字节的对象
【问题讨论】:
-
请格式化您的代码 - 这样不可读。
标签: python json beautifulsoup urllib