urllib.error.HTTPError: HTTP Error 400: Bad Request in Python function

urllib.error.HTTPError: HTTP Error 400: Bad Request in Python function

我正在制作一个程序,它根据特定输入(到目前为止是成分)搜索食谱。当我只搜索一些成分时,该程序可以工作,但还有一些 return 一个 urllib 错误。我查看了其他问题,但它们是针对 urllib 2 的,他们的解决方案并没有解决我的问题。

Link 有效的地方(搜索一些成分)- http://allrecipes.com/search/results/?ingIncl=Chicken&ingExcl=beef&sort=re

Link 哪里没有(搜索更多)- http://allrecipes.com/search/results/?ingIncl=chicken,cheese,egg&ingExcl=lettuce&sort=re

*下面是我的代码

from bs4 import BeautifulSoup
import urllib
import urllib.request


html = "http://allrecipes.com/search/results/?ingIncl='chicken', 'cheese', 'egg'&ingExcl='lettuce'&sort=re"
number = 5



def get_recipes(html, number):######## this doesnt work if there are a few ingredients

    html = urllib.request.urlopen(html)

    soup = BeautifulSoup(html, "html.parser")

    num_results = soup.find('span',{'class': 'subtext'}).get_text()
    num_results = str(number) + ' out of ' + num_results #number will have to be changed if less recipes were found than number

    i = 0
    recipe_dict = {}


    for card in soup.find_all('article', {'class':'grid-col--fixed-tiles'}): #gets 2 more than required
        try: 
            info = card.find('a', {'data-internal-referrer-link':'hub recipe'})
            link = info.get('href')
            name = info.get_text()
            recipe_dict[name] = link
            if i > (number - 2): #-2 is temp fix
                break
            else:
                i += 1
        except:
            pass
    print(recipe_dict)
    return recipe_dict

get_recipes(html, number)

错误:

Traceback (most recent call last):
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\Diet Buddy\DB_find_recipes.py", line 39, in <module>
    get_recipes(html, number)
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\Diet Buddy\DB_find_recipes.py", line 13, in get_recipes
    html = urllib.request.urlopen(html)
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 472, in open
    response = meth(req, response)
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 582, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 510, in error
    return self._call_chain(*args)
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 444, in _call_chain
    result = func(*args)
  File "C:\Users\bakat\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 590, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

似乎是一个简单的错字。比较:

html = "http://allrecipes.com/search/results/?ingIncl='chicken', 'cheese', 'egg'&ingExcl='lettuce'&sort=re"

html = "http://allrecipes.com/search/results/?ingIncl=chicken,cheese,egg&ingExcl=lettuce&sort=re"