YOLO-series icon indicating copy to clipboard operation
YOLO-series copied to clipboard

HTTPError: HTTP Error 400: Bad Request

Open akankshaprasad8 opened this issue 5 years ago • 1 comments

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.decode("utf-8")) 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 = 'fidget kid spinner toys' links = get_links(search_name) save_images(links, search_name)

error after running the above code:


HTTPError Traceback (most recent call last) in 37 if name == 'main': 38 search_name = 'fidget kid spinner toys' ---> 39 links = get_links(search_name) 40 save_images(links, search_name)

in get_links(search_name) 17 url = url_base.format(search_name, 0) 18 request = ulib.Request(url, None, headers) ---> 19 json_string = ulib.urlopen(request).read() 20 page = json.loads(json_string.decode("utf-8")) 21 new_soup = Soup(page[1][1], 'lxml')

C:\ProgramData\Anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) 220 else: 221 opener = _opener --> 222 return opener.open(url, data, timeout) 223 224 def install_opener(opener):

C:\ProgramData\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout) 529 for processor in self.process_response.get(protocol, []): 530 meth = getattr(processor, meth_name) --> 531 response = meth(req, response) 532 533 return response

C:\ProgramData\Anaconda3\lib\urllib\request.py in http_response(self, request, response) 639 if not (200 <= code < 300): 640 response = self.parent.error( --> 641 'http', request, response, code, msg, hdrs) 642 643 return response

C:\ProgramData\Anaconda3\lib\urllib\request.py in error(self, proto, *args) 567 if http_err: 568 args = (dict, 'default', 'http_error_default') + orig_args --> 569 return self._call_chain(*args) 570 571 # XXX probably also want an abstract factory that knows when it makes

C:\ProgramData\Anaconda3\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args) 501 for handler in handlers: 502 func = getattr(handler, meth_name) --> 503 result = func(*args) 504 if result is not None: 505 return result

C:\ProgramData\Anaconda3\lib\urllib\request.py in http_error_default(self, req, fp, code, msg, hdrs) 647 class HTTPDefaultErrorHandler(BaseHandler): 648 def http_error_default(self, req, fp, code, msg, hdrs): --> 649 raise HTTPError(req.full_url, code, msg, hdrs, fp) 650 651 class HTTPRedirectHandler(BaseHandler):

please help

akankshaprasad8 avatar Jun 22 '19 20:06 akankshaprasad8

Any solutions as of now??

DynamicCodes avatar Feb 25 '20 11:02 DynamicCodes