python - 试图从图像搜索中获取 url
问题描述
有人可以帮我解决这个代码!所以我想做的是制作一个程序,如果你输入一个单词,它就会找到第一张图片并从img发回url,但它不会那样做。
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
word = input()
html = urlopen('https://www.google.com/search?q=', word +'&rlz=1C1GCEU_lvLV926LV926&sxsrf=ALeKk01xl0HutDOTshkCUPM5qDFtKyvuKg:1613851219348&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjC0JiloPnuAhWoAxAIHZKdAGUQ_AUoAXoECA4QAw&biw=958&bih=959')
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src':re.compile('.jpg')})
for image in images:
print(image['src']+'\n')
有人可以解释我该怎么做
解决方案
看起来有些图像已编码,但试试这个。如果图像经过编码,您可能在 src 或 href 的任何地方都找不到 .jpg。
url = 'https://www.google.com/search?q=guitar'
page = requests.get(url)
soup = BeautifulSoup(page.text, "html.parser")
images = soup.find_all(href=re.compile('.jpg'))
for image in images:
print(image.get('href'))
它会拉出一些图片网址:
https://www.google.com/imgres?imgurl=https://cdn.mos.cms.futurecdn.net/Ge25ccbyKQ76Et9bBjFnxk-1200-80.jpg&imgrefurl=https://www.guitarworld.com/gear/types-of-guitar-everything-you-need-to-know&h=675&w=1200&tbnid=1bWm5qMm6P85iM&q=guitar&tbnh=84&tbnw=150&usg=AI4_-kR-ixXbUq1jFtJ-kcukVj6j-7KgTw&vet=1&docid=4ZL7MkOS7tG24M&sa=X&ved=2ahUKEwi0qaL_pvnuAhUCXK0KHYLrCWUQ9QEwJHoECAEQCA
https://www.google.com/imgres?imgurl=https://online.berklee.edu/takenote/wp-content/uploads/2020/07/learn_acoustic_blues_guitar_article_image.jpg&imgrefurl=https://online.berklee.edu/takenote/acoustic-blues-guitar-tips/&h=1200&w=1920&tbnid=QR9aabuUf_XeFM&q=guitar&tbnh=94&tbnw=150&usg=AI4_-kSKaX2goL8QU_gf6aNPMvEK3WF3tw&vet=1&docid=hdq2fzc2ogCnkM&sa=X&ved=2ahUKEwi0qaL_pvnuAhUCXK0KHYLrCWUQ9QEwJXoECAEQCg
https://www.google.com/imgres?imgurl=https://images-na.ssl-images-amazon.com/images/I/41jIw1mUV4L._AC_.jpg&imgrefurl=https://www.amazon.com/Yamaha-FG800-Solid-Acoustic-Guitar/dp/B01C92QHLC&h=500&w=204&tbnid=ESB5AJN1MKnK_M&q=guitar&tbnh=130&tbnw=53&usg=AI4_-kQB83ftunCPyX3cXobwJMp0b1UhAg&vet=1&docid=9Ld6uZPysxav6M&sa=X&ved=2ahUKEwi0qaL_pvnuAhUCXK0KHYLrCWUQ9QEwJnoECAEQDA
推荐阅读
- python - os.environ windows 与树莓派
- android - 在 Kotlin 中从 sqliteDB 填充 Spinner 数据
- python - 使用 SVM 对 5 维数据集进行分类的示例?
- sql - 如何在 DB2 数据库中查找 char 0x1A (→)
- css - 我可以在一个项目中拥有一个可以在多个项目中导入的通用 SCSS
- visual-studio - 如何在 Xamarin Visual Studio 2019 中分析应用程序性能
- python - 我的 Cython 代码与 Python 交互过多
- react-native - 换行(多行)按钮
- python - 为什么我的数据框在连接时会创建新行?
- azure - 在运行 az cosmosdb create 命令创建 Cosmos DB 帐户时如何指定 MongoDB API 版本?