0
特定の入力(これまでの成分)に基づいてレシピを検索するプログラムを作成しています。このプログラムは、ほんの少しの食材だけを検索するときに機能しますが、さらにいくつかはurllibエラーを返します。私は他の質問を見ましたが、それらはurllib 2用であり、その解決策は私の問題を解決しませんでした。urllib.error.HTTPError:HTTPエラー400:Python関数のリクエストが不正です
それが動作リンク(少数の食材を探して) - それは(もっと探して)いないhttp://allrecipes.com/search/results/?ingIncl=Chicken&ingExcl=beef&sort=re
リンク - 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