首页 > 解决方案 > I'm trying to make an API in python that receives data in JSON

问题描述

I'm trying to make an API which depending on which store the user likes and what specific item they like it will retrieve the data in JSON, for example, in Tesco search for Nutella, it works perfectly to find when I do it for just 1 store but when I did it for more than 1 it get errors.

I've tried a for loop and if loop, I've gotten better results from bs4 import BeautifulSoup libraries to do this.

My code:

import requests
from bs4 import BeautifulSoup

num = int(input('''press 1 for tesco
press 2 for morrisions
press 3 for sainsbury's
'''))

print("please enter the food you want to search for: ")
flag = num%2
txt = input("")

if flag == 1:
    tesco_url = requests.get('https://www.tesco.com/groceries/en-GB/search?query=' + txt).text
elif flag == 2:
    morrisons_url = requests.get('https://groceries.morrisons.com/search?entry=' + txt).text
elif flag == 3:
    sainsburys_url = requests.get('https://www.sainsburys.co.uk/webapp/wcs/stores/servlet/SearchDisplayView?catalogId=10123&langId=44&storeId=10151&krypto=OYaxfyCjgnRApUa%2FS%2BjRmgHslGfDEUtd3xECMndoz2f9gvq5KRuP8TuhW4m1jnUT%2FJU3fBivUiAIozuhmBLJJJQe6gcTedPJTASuwsZfLkt49e%2FYAPxDyWxCjeiyFxNN5WjSEdcW7LMdmfJbn3TmGVBZKVIxqu1zUw7IT8Qo2afgUyuCpJPcxPbmc2gWJMpi#langId=44&storeId=10151&catalogId=10123&categoryId=&parent_category_rn=&top_category=&pageSize=60&orderBy=RELEVANCE&searchTerm=' + txt).text

soup = BeautifulSoup(flag, 'lxml')
print(soup.prettify())

The error I'm getting:

Traceback (most recent call last):
  File "My API.py", line 23, in <module>
    soup = BeautifulSoup(flag, 'lxml')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/__init__.py", line 245, in __init__
    elif len(markup) <= 256 and (
TypeError: object of type 'int' has no len()

标签: pythonpython-requests

解决方案


BeautifulSoup 需要 XML,您可能在添加更多商店时忘记更新它。另外我不确定你为什么有flag = num%2,因为 flag 将只是值 [0, 1]。如果您替换flagnum,它应该可以按预期工作。

import requests
from bs4 import BeautifulSoup

num = int(input('''press 1 for tesco
press 2 for morrisions
press 3 for sainsbury's
'''))

print("please enter the food you want to search for: ")
flag = num                   # <--- notice this
txt = input("")

if flag == 1:
    markup = requests.get('https://www.tesco.com/groceries/en-GB/search?query=' + txt).text
elif flag == 2:
    markup = requests.get('https://groceries.morrisons.com/search?entry=' + txt).text
elif flag == 3:
    markup = requests.get('https://www.sainsburys.co.uk/webapp/wcs/stores/servlet/SearchDisplayView?catalogId=10123&langId=44&storeId=10151&krypto=OYaxfyCjgnRApUa%2FS%2BjRmgHslGfDEUtd3xECMndoz2f9gvq5KRuP8TuhW4m1jnUT%2FJU3fBivUiAIozuhmBLJJJQe6gcTedPJTASuwsZfLkt49e%2FYAPxDyWxCjeiyFxNN5WjSEdcW7LMdmfJbn3TmGVBZKVIxqu1zUw7IT8Qo2afgUyuCpJPcxPbmc2gWJMpi#langId=44&storeId=10151&catalogId=10123&categoryId=&parent_category_rn=&top_category=&pageSize=60&orderBy=RELEVANCE&searchTerm=' + txt).text

soup = BeautifulSoup(markup, 'lxml')
print(soup.prettify())

推荐阅读