如何使用 Beautiful Soup 在 Google 图片上查找 URL

问题描述

我试图在 Google 上查找无版权的图片,但无法获得正确的图片网址。我的代码应用了正确的过滤器并将我定向到正确的页面,但它检索了没有无版权和大小过滤器的图像的 URL,我不确定为什么。提前致谢。

import requests
import urllib.request
from bs4 import BeautifulSoup
from urllib.request import urlopen,Request

url = 'https://google.com/search?q='
input = 'cat'
#string: tbm=isch --> means image search
#tbs=isz:m --> size medium
#il:cl --> copy right free(i think)
url = url+input+'&tbm=isch&tbs=isz:m%2Cil:cl'
print(url)
html = urlopen(Request(url,headers={'User-Agent': 'Google Chrome'}))
'''with urllib.request.urlopen(url) as response:
    html = response.read()
    print(html)'''
#print(str(r.content))

soup = BeautifulSoup(html.read(),'html.parser')

#using soup to find all img tags
results = soup.find_all('img')
str_result = str(results)

lst_result = str_result.split(',')
#trying to get the first link for the images with the appropriate settings
link = lst_result[4].split(' ')[4].split('"')[1]

# writing into the appropriate testing file,to be changed
file = open('.img1.png','wb')
get_img = requests.get(link)
file.write(get_img.content)
file.close()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...