gpt4 book ai didi

python - urllib.error.HTTPError : HTTP Error 400: Bad Request from trying to get a set of images

转载 作者:行者123 更新时间:2023-12-01 09:14:54 29 4
gpt4 key购买 nike

我试图获取一组图像作为数据集来训练tiny-yolo,所以我遵循了本教程

https://www.youtube.com/watch?v=Lg4T9iJkwhE&index=5&list=PLX-LrBk6h3wSGvuTnxB2Kj358XfctL4BM

教程中编写的第一个文件如下所示:

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/67.0.3396.99 Safari/537.36'}


def get_links(search_name):
search_name = search_name.replace(' ', '+')
url = url_base.format(search_name, 0)
request = ulib.Request(url, None, 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


def save_images(links, search_name):
directory = search_name.replace(' ', '_')
if not os.path.isdir(directory):
os.mkdir(directory)

for i, link in enumerate(links):
savepath = os.path.join(directory, '{:06}.png'.format(i))
ulib.urlretrieve(link, savepath)


if __name__ == '__main__':
search_name = 'my search query'
links = get_links(search_name)
save_images(links, search_name)

我唯一改变的是 headers 变量,因为我的用户代理与教程中的用户代理不同。

令我惊讶的是脚本返回了这个错误

urllib.error.HTTPError: HTTP Error 400: Bad Request

谁能告诉我怎么了?

最佳答案

错误代码本身就说明了问题
您正在尝试访问不存在的 URL。

请更正您的网址。 enter image description here

网址:- https://www.google.com/search?ei=1m7NWePfFYaGmQG51q7IBg&hl=en&q=my+search+query\\&tbm=isch&ved=0ahUKEwjjovnD7sjWAhUGQyYKHTmrC2kQuT0I7gEoAQ&start=0\\&yv=2&vet=10ahUKEwjjo vnD7sjWAhUGQyYKHTmrC2kQuT0I7gEoAQ.1m7NWePffYaGmQG51q7IBg\\.i&ijn=1&asearch=ichunk&async=_id:rg_s,_pms:s%27

尝试使用浏览器,看看会得到什么。

关于python - urllib.error.HTTPError : HTTP Error 400: Bad Request from trying to get a set of images,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51353604/

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