我正在尝试学习python。我的第一个项目之一是从Google图片下载图片。我遇到了两个问题:
1:如果单击DataFrame对象中的链接,则只能获得1x1分辨率的图片。为什么?
2:如果仅打印URL,则可得到20个结果,但是如果使用下载功能,则仅下载两个。再说一次,为什么?
from bs4 import BeautifulSoup
import requests
import pandas as pd
import urllib.request
def dl_jpg(url, file_path,file_name):
full_path =file_path +file_name + '.jpg'
urllib.request.urlretrieve(url,full_path)
search = 'car'
url = 'https://www.google.co.in/search?q='+search+'&source=lnms&tbm=isch'
page = requests.get(url)
soup = BeautifulSoup(page.content,'html.parser')
images = soup.find_all("img")
frame = pd.DataFrame({
'url': [image['src'] for image in images]
}
)
for image in images:
print(image['src'])
x = 0
for image in images:
dl_jpg(image['src'],'images/',str(x))
x =+1