I am trying to get create a dictionary of categories especially a food from this url. Now, when I am trying to use the below code, it is giving repetitive li
items.
from bs4 import BeautifulSoup as bs
import requests
url = 'https://developer.foursquare.com/docs/build-with-foursquare/categories/'
req = requests.get(url)
soup = bs(req.text)
food_categories = soup.select('div.documentTemplate__Content-sc-5mpekp-0 > ul > li:nth-child(4)')[0]
for tagli in food_categories.find_all("li"):
print(tagli.find('h3').text)
for another_tagli in tagli.find_all('ul'):
for some_tagli in another_tagli.find_all('li'):
print(some_tagli.find('h3').text)
for one_tagli in some_tagli.find_all('ul'):
for aon_tagli in one_tagli.find_all('li'):
print(aon_tagli.find('h3').text)
Now, as per many stackoverflow posts, I have tried to use recursive=False
argument to get the only direct children, but if I use that I get nothing.
我正在寻找这样的输出:
{
'food': {
'Afghan Restaurant': [],
'African Restaurant': ['Ethiopian Restaurant'],
'Asian Restaurant': {
'Chinese Restaurant': ['Anhui Restaurant', 'Beijing Restaurant']
}
}
}
请在这里指导我。
此脚本从“食物”子类别生成树:
印刷品: