首页 > 解决方案 > 试图从图像搜索中获取 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')

有人可以解释我该怎么做

标签: pythonimageurlbeautifulsoup

解决方案


看起来有些图像已编码,但试试这个。如果图像经过编码,您可能在 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

推荐阅读