Hassan Waris
Hassan Waris

Reputation: 11

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()

Upvotes: 0

Views: 256

Answers (1)

Jeppe
Jeppe

Reputation: 2296

BeautifulSoup requires XML, which you probably forgot to update when adding more stores. Also I'm not sure why you have flag = num%2, as flag will then only be values [0, 1]. If you replace flag with num, it should work as expected.

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())

Upvotes: 1

Related Questions