gpt4 book ai didi

python - 尝试使用 Google 搜索搜索图片,错误 400

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:41 27 4
gpt4 key购买 nike

我不断收到此错误:urllib.error.HTTPError:HTTP 错误 400:错误请求

我相信这可能与链接有关,因为当我将它们放入(并替换 {})时,我收到相同的错误,但我不知道哪些链接是正确的/(Python 3.6、Anaconda)

import os
import urllib.request as ulib
from bs4 import BeautifulSoup as Soup
import json

url_a = 'https://www.google.com/search?ei=1m7NWePfFYaGmQG51q7IBg&hl=en&q={}'
url_b = '\&tbm=isch&ved=0ahUKEwjjovnD7sjWAhUGQyYKHTmrC2kQuT0I7gEoAQ&start={}'
url_c = '\&yv=2&vet=10ahUKEwjjovnD7sjWAhUGQyYKHTmrC2kQuT0I7gEoAQ.1m7NWePfFYaGmQG51q7IBg'
url_d = '\.i&ijn=1&asearch=ichunk&async=_id:rg_s,_pms:s'
url_base = ''.join((url_a, url_b, url_c, url_d))

headers = {'User-Agent': 'Chrome/69.0.3497.100'}

def get_links(search_name):
search_name = search_name.replace(' ', '+')
url = url_base.format(search_name, 0)
request = ulib.Request(url, data=None, headers=headers)
json_string = ulib.urlopen(request).read()
page = json.loads(json_string)
new_soup = Soup(page[1][1], 'lxml')
images = new_soup.find_all('img')
links = [image['src'] for image in images]
return links

if __name__ == '__main__':
search_name = 'Thumbs up'
links = get_links(search_name)

for link in links:
print(link)

最佳答案

我认为你有很多不需要的参数

尝试使用这个更简单的 URL 进行图像搜索:

https://www.google.com/search?q={KEY_WORD}&tbm=isch

例如:

https://www.google.com/search?q=apples&tbm=isch

关于python - 尝试使用 Google 搜索搜索图片,错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52970970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com