【发布时间】:2021-05-23 12:50:22
【问题描述】:
谁能帮我解决这个代码!所以我想做的是制作一个程序,如果你输入一个单词,它会找到第一张图片并从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')
谁能解释我该怎么做
【问题讨论】:
-
轻微备注,您可以将您的地址简化为
"https://www.google.com/search?tbm=isch&q=" + word。因为它(可能)会暴露您的个人数据并且是多余的。
标签: python image url beautifulsoup