python - 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()
解决方案
BeautifulSoup 需要 XML,您可能在添加更多商店时忘记更新它。另外我不确定你为什么有flag = num%2
,因为 flag 将只是值 [0, 1]。如果您替换flag
为num
,它应该可以按预期工作。
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())
推荐阅读
- android - 如何从另一个活动刷新片段?
- r - 如果 A 列中的因子与 B 列中的因子匹配,如何删除行
- javascript - 关于 JavaScript Math.random() 和基本逻辑的问题
- asp.net-core - 将值从一个动作发送到另一个动作
- arrays - 我如何将数组的值收集为数字?
- java - 来自 Java 应用程序(但不是 Web 浏览器)的 HTTP 403,尽管设置了 User-Agent
- algorithm - FFT算法的差异
- javascript - 如何修复不加载 iframe 的 Android 应用程序?
- android - TabLayout 的 PressedListener 事件
- python - 在 mac 10.11.6 上安装 Tensorflow