urllib.error.HTTPError:HTTP 错误 400:Python 函数中的错误请求

哈里·汤

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

链接有效的地方(搜索一些成分)-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
安东尼·索蒂尔

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

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"

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

再次urllib.error.HTTPError:HTTP错误400:错误的请求

来自分类Dev

urllib2.HTTPError:HTTP错误400:错误的请求-Python

来自分类Dev

urllib.error.HTTPError:HTTP错误502:错误的网关PYTHON

来自分类Dev

Python urllib.error.HTTPError:HTTP 错误 404:未找到

来自分类Dev

美丽的汤-urllib.error.HTTPError:HTTP错误403:禁止

来自分类Dev

urllib.error.HTTPError:HTTP错误403:禁止urlretrieve

来自分类Dev

urllib.error.HTTPError:HTTP 错误 404:未找到

来自分类Dev

urllib.error.HTTPError:HTTP错误404:从Metacritic抓取数据时未找到Python

来自分类Dev

Python 3.6.2 url.request.urlopen() urllib.error.HTTPError:HTTP 错误 403:禁止

来自分类Dev

PY中的Google API请求错误:“ urllib.error.HTTPError:HTTP错误500:内部服务器错误”

来自分类Dev

python,tweepy:urllib2.HTTPError:HTTP错误403:禁止

来自分类Dev

Python:urllib2.HTTPError:HTTP错误300:多项选择

来自分类Dev

HTTPError:HTTP错误400:错误的请求

来自分类Dev

异常值:HTTP错误400:对PayPal沙盒网站的Python / urllib2请求后,错误请求

来自分类Dev

发送带有cookie的发布请求时遇到问题---- urllib.error.HTTPError:HTTP错误503:服务不可用

来自分类Dev

python urllib.error.HTTPError

来自分类Dev

urllib2 HTTPError:HTTP错误414:Request-URI太大

来自分类Dev

从urlReq(url)中删除'urllib.error.HTTPError:HTTP Error 302:'

来自分类Dev

HttpError 400错误请求-Google Admin Directory API(Python)

来自分类Dev

使用Ruby提取推文将返回“ open_http”:400错误请求(OpenURI :: HTTPError)

来自分类Dev

HTTP请求:400错误

来自分类Dev

Python urllib2请求错误

来自分类Dev

带有Python瓶颈的“ HTTP错误400:错误的请求”

来自分类Dev

AJAX POST请求中的HTTP错误400

来自分类Dev

urllib错误的请求问题

来自分类Dev

python请求400错误

来自分类Dev

Python 3.4 urllib.request错误(http 403)

来自分类Dev

Python-urllib2>如何转义HTTP错误

来自分类Dev

Python urllib2.urlopen返回HTTP错误503

Related 相关文章

  1. 1

    再次urllib.error.HTTPError:HTTP错误400:错误的请求

  2. 2

    urllib2.HTTPError:HTTP错误400:错误的请求-Python

  3. 3

    urllib.error.HTTPError:HTTP错误502:错误的网关PYTHON

  4. 4

    Python urllib.error.HTTPError:HTTP 错误 404:未找到

  5. 5

    美丽的汤-urllib.error.HTTPError:HTTP错误403:禁止

  6. 6

    urllib.error.HTTPError:HTTP错误403:禁止urlretrieve

  7. 7

    urllib.error.HTTPError:HTTP 错误 404:未找到

  8. 8

    urllib.error.HTTPError:HTTP错误404:从Metacritic抓取数据时未找到Python

  9. 9

    Python 3.6.2 url.request.urlopen() urllib.error.HTTPError:HTTP 错误 403:禁止

  10. 10

    PY中的Google API请求错误:“ urllib.error.HTTPError:HTTP错误500:内部服务器错误”

  11. 11

    python,tweepy:urllib2.HTTPError:HTTP错误403:禁止

  12. 12

    Python:urllib2.HTTPError:HTTP错误300:多项选择

  13. 13

    HTTPError:HTTP错误400:错误的请求

  14. 14

    异常值:HTTP错误400:对PayPal沙盒网站的Python / urllib2请求后,错误请求

  15. 15

    发送带有cookie的发布请求时遇到问题---- urllib.error.HTTPError:HTTP错误503:服务不可用

  16. 16

    python urllib.error.HTTPError

  17. 17

    urllib2 HTTPError:HTTP错误414:Request-URI太大

  18. 18

    从urlReq(url)中删除'urllib.error.HTTPError:HTTP Error 302:'

  19. 19

    HttpError 400错误请求-Google Admin Directory API(Python)

  20. 20

    使用Ruby提取推文将返回“ open_http”:400错误请求(OpenURI :: HTTPError)

  21. 21

    HTTP请求:400错误

  22. 22

    Python urllib2请求错误

  23. 23

    带有Python瓶颈的“ HTTP错误400:错误的请求”

  24. 24

    AJAX POST请求中的HTTP错误400

  25. 25

    urllib错误的请求问题

  26. 26

    python请求400错误

  27. 27

    Python 3.4 urllib.request错误(http 403)

  28. 28

    Python-urllib2>如何转义HTTP错误

  29. 29

    Python urllib2.urlopen返回HTTP错误503

热门标签

归档