引发HTTPError(req.full_url,code,msg,hdrs,fp)urllib.error.HTTPError:HTTP错误400:错误的请求

时间:2018-06-25 01:32:15

标签: python json yolo

因此,我试图按照本教程https://www.youtube.com/watch?v=Lg4T9iJkwhE&t=155s进行YOLO图像检测。

,并且在尝试运行此代码以自动下载手机图像但不起作用

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/41.0.2228.0 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 = 'cell phones'
    links = get_links(search_name)
    save_images(links, search_name)

我遇到了很多这样的错误:

C:\Python36\python.exe "C:/dark/darkflow-master/new_model_data/part5 - get_images.py"
Traceback (most recent call last):
  File "C:/dark/darkflow-master/new_model_data/part5 - get_images.py", line 39, in <module>
    links = get_links(search_name)
  File "C:/dark/darkflow-master/new_model_data/part5 - get_images.py", line 19, in get_links
    json_string = ulib.urlopen(request).read()
  File "C:\Python36\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python36\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Python36\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python36\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Python36\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Python36\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

Process finished with exit code 1

有人请帮我解决这个问题,谢谢。

0 个答案:

没有答案