How to find specific HTML element after CSS selector with BeautifulSoup? - python

I am trying to retrieve the last img scr of a web page doing webscraping with BeautifulSoup. So far I am trying to use a selector but it if impossible for me to find anything after the ::before selector.
The basic code is:
import requests
from bs4 import BeautifulSoup
s = requests.session()
r = s.get("https://www.immobiliare.it/vendita-case/milano/forlanini/?criterio=dataModifica&ordine=desc")
soup = BeautifulSoup(r.content, "lxml")
for property in soup.find_all("li", {"class": "nd-list__item in-realEstateResults__item"}):
The HTML code of the page has the following structure:
Each li class="nd-list__item in-realEstateResults__item" is a property I want to extract the img src from.
Bear in mind that the first image has an easier html code, I cannot get the src from the rest of them

EDIT
I have to correct my initial statement:
The use of the rather sluggish selenium is not absolutely necessary and it is also possible to implement it using requests and beautifulsoup.
On closer inspection, it turned out that all the information can be found in a <script>. Its content can be extracted and used as JSON, and the urls of the maps have to be assembled based on the location information.
Example
import requests, json, time
from bs4 import BeautifulSoup
data = []
url = f'https://www.immobiliare.it/vendita-case/milano/forlanini/?criterio=dataModifica&ordine=desc'
while True:
jsonData = json.loads(
BeautifulSoup(
requests.get(url).text
).select_one('#__NEXT_DATA__').text
)['props']['pageProps']['dehydratedState']['queries'][0]['state']['data']['pages'][0]
for e in jsonData['results']:
l = e['realEstate']['properties'][0]['location']
data.append({
'id':e['realEstate']['id'],
'map':f"https://maps.im-cdn.it/static?zoom=15&size=360x270&language=it&style=feature%3Aroad%7Celement%3Alabels%7Cvisibility%3Aoff&sensor=false&markers=icon%3Ahttps%3A%2F%2Fs1.immobiliare.it%2F_next%2Fstatic%2Fmedia%2Fmap-marker.27fc2b6f.png%7C{l['latitude']}%2C{l['longitude']}&center={l['latitude']}%2C{l['longitude']}"
})
print(f"scraping page: {jsonData['currentPage']}")
if jsonData['maxPages'] != jsonData['currentPage']:
url = f"https://www.immobiliare.it/vendita-case/milano/forlanini/?criterio=dataModifica&ordine=desc&pag={jsonData['currentPage']+1}"
else:
break
time.sleep(1)
data
Older Answer
As mentioned in the comments, content is rendered dynamically, so you will not get the expected result in used combination of requests, that will not render JS, like a browser will do, and BeautifulSoup that won`t find your expected elements, cause they are not there.
Just to clarify ::before is a pseudo-element:
In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.
You could go with requests if you will use an api, some information comes from:
s.get('https://www.immobiliare.it/api-next/agencies/local-expert/?city-id=8042&province-id=MI&macrozone-id[0]=10294&limit=25&output=json').json()
-> {'agencies': [{'id': 9681, 'displayName': 'Fonte Immobiliare Città Studi 2', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/934856533.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/934856531.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/9681/fonte-citta-studi--milano/', 'address': 'Via Giovanni Briosi 10 20133 - Milano', 'bannerImage': 'https://pic.im-cdn.it/image/934857363/xs-c.jpg', 'externalId': None, 'timeContract': 11, 'paid': True}, {'id': 83565, 'displayName': 'Cfc Immobiliare ', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/244109821.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/244109817.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/83565/cfc-milano/', 'address': 'Via Carnia 7 20132 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.492900,9.236340&zoom=15&size=400x230&markers=45.492900,9.236340', 'externalId': None, 'timeContract': 10, 'paid': True}, {'id': 208668, 'displayName': 'YOUR HOME - Real Estate', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1127494478.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1127494476.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/208668/your-home-milano/', 'address': 'Bastioni Porta Nuova 21 20121 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.480100,9.188150&zoom=15&size=400x230&markers=45.480100,9.188150', 'externalId': None, 'timeContract': 7, 'paid': True}, {'id': 231505, 'displayName': 'Homepanda', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/693409659.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/693409657.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/231505/homepanda/', 'address': 'Via Gian Giacomo Mora 20 20123 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.458900,9.179330&zoom=15&size=400x230&markers=45.458900,9.179330', 'externalId': None, 'timeContract': 4, 'paid': True}, {'id': 118081, 'displayName': 'CONSULOVEST CORBETTA Via Meroni 2 - MILANO V.le San Gimignano 8', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1162882814.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1162882812.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/118081/consulovest-corbetta/', 'address': 'Via Meroni 2 20011 - Corbetta', 'bannerImage': 'https://pic.im-cdn.it/image/1162882818/xs-c.jpg', 'externalId': None, 'timeContract': None, 'paid': False}, {'id': 5272, 'displayName': 'Arena Immobiliare S.R.L.', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/936162495.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/936162493.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/5272/arena-milano/', 'address': 'Via Marco Bruto 9 20138 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.459800,9.238870&zoom=15&size=400x230&markers=45.459800,9.238870', 'externalId': None, 'timeContract': 21, 'paid': False}, {'id': 32741, 'displayName': 'Studio emme3 ', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/196647202.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/196647201.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/32741/studio-emme-milano/', 'address': 'Via Pompeo Neri 2 20146 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.456800,9.143770&zoom=15&size=400x230&markers=45.456800,9.143770', 'externalId': None, 'timeContract': 4, 'paid': False}, {'id': 242120, 'displayName': 'Levia SRL', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/843934046.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/843934044.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/242120/levia-milano/', 'address': 'Viale Ungheria 20 20138 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.445700,9.246040&zoom=15&size=400x230&markers=45.445700,9.246040', 'externalId': None, 'timeContract': 3, 'paid': False}, {'id': 396994, 'displayName': 'Affiliato Tecnorete: STUDIO IMMOBILIARE CORSICA SRL', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1247888668.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1247888664.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/396994/tecnorete-milano-viale-ungheria/', 'address': 'Viale Ungheria 24 20135 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.445500,9.246760&zoom=15&size=400x230&markers=45.445500,9.246760', 'externalId': None, 'timeContract': 0, 'paid': False}, {'id': 140950, 'displayName': 'Abitare Agency Srl', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1135165888.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1135165886.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/140950/abitare-agency/', 'address': 'Via Voghera 7 20144 - Milano', 'bannerImage': 'https://pic.im-cdn.it/image/1135165932/xs-c.jpg', 'externalId': None, 'timeContract': 10, 'paid': False}, {'id': 94305, 'displayName': 'Affiliato Tecnocasa: IMMOBILIARE MARGOT SRLU', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1135591154.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1135591152.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/94305/tecnocasa-milano-via-mecenate/', 'address': 'Via Mecenate 4 20138 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.457400,9.242440&zoom=15&size=400x230&markers=45.457400,9.242440', 'externalId': None, 'timeContract': 8, 'paid': False}, {'id': 241224, 'displayName': 'INVIMIT SGR SpA', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/829818360.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/829818358.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/241224/invimit-roma/', 'address': 'Via di Santa Maria in Via 12 00187 - Roma', 'bannerImage': 'https://pic.im-cdn.it/image/825106468/xs-c.jpg', 'externalId': None, 'timeContract': 3, 'paid': False}, {'id': 209778, 'displayName': 'STUDIO6ERRE - Sede Milano', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/937464013.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/937464011.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/209778/studioerre-milano/', 'address': 'Viale Abruzzi 80 20131 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.483700,9.217150&zoom=15&size=400x230&markers=45.483700,9.217150', 'externalId': None, 'timeContract': 7, 'paid': False}, {'id': 166328, 'displayName': 'HB ADVISORY', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/311765482.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/311765478.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/166328/hb-advisory/', 'address': 'Corso Buenos Aires 60 20124 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.482200,9.212750&zoom=15&size=400x230&markers=45.482200,9.212750', 'externalId': None, 'timeContract': 9, 'paid': False}, {'id': 41477, 'displayName': 'StudioZimer', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1143272706.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1143272704.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/41477/studiozimer/', 'address': 'CORSO LODI 111 20135 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.441500,9.221210&zoom=15&size=400x230&markers=45.441500,9.221210', 'externalId': None, 'timeContract': 4, 'paid': False}, {'id': 386016, 'displayName': 'STUDIO ASTE MC', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1250133574.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1250133572.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/386016/studio-aste-mc-sesto-san-giovanni/', 'address': 'Via Carlo Cattaneo 49 20099 - Sesto San Giovanni', 'bannerImage': 'https://pic.im-cdn.it/image/1147591370/xs-c.jpg', 'externalId': None, 'timeContract': None, 'paid': False}, {'id': 42941, 'displayName': 'OBIETTIVOCASA', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/155958486.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/155958482.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/42941/obiettivocasa-milano-via-pordenone/', 'address': 'via pordenone 13 20132 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.490300,9.234840&zoom=15&size=400x230&markers=45.490300,9.234840', 'externalId': None, 'timeContract': 10, 'paid': False}, {'id': 392582, 'displayName': 'AsteGlobal', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1227120896.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1227120894.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/392582/asteblobal/', 'address': 'via Reali 13 20037 - Paderno Dugnano', 'bannerImage': 'https://pic.im-cdn.it/image/1227121058/xs-c.jpg', 'externalId': None, 'timeContract': None, 'paid': False}, {'id': 203747, 'displayName': 'Le case di Patty', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/811914140.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/811914138.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/203747/le-case-di-patty-milano/', 'address': 'Via Montebello 14 20121 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.475200,9.189070&zoom=15&size=400x230&markers=45.475200,9.189070', 'externalId': None, 'timeContract': 7, 'paid': False}, {'id': 35498, 'displayName': "Expo' Servizi Immobiliari", 'imageUrls': [], 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/35498/expo/', 'address': 'Viale Premuda 21 20129 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.466000,9.207020&zoom=15&size=400x230&markers=45.466000,9.207020', 'externalId': None, 'timeContract': 5, 'paid': False}, {'id': 228450, 'displayName': 'Aste Milano Immobiliare', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1061930169.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1061930167.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/228450/aste-rozzano/', 'address': 'Via Innocenzo Isimbardi 29 20141 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.435200,9.180890&zoom=15&size=400x230&markers=45.435200,9.180890', 'externalId': None, 'timeContract': 5, 'paid': False}, {'id': 5350, 'displayName': 'IMI immobiliare Milano - Partner Navigli', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/424092179.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/424092177.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/5350/imi-milano-navigli/', 'address': 'Via Conchetta 2 20136 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.446800,9.179270&zoom=15&size=400x230&markers=45.446800,9.179270', 'externalId': None, 'timeContract': 12, 'paid': False}, {'id': 237934, 'displayName': 'ASTA4YOU', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1112140922.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1112140920.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/237934/astayou/', 'address': 'Via Domenico Cimarosa 26 20144 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.464200,9.157880&zoom=15&size=400x230&markers=45.464200,9.157880', 'externalId': None, 'timeContract': 3, 'paid': False}, {'id': 28201, 'displayName': 'Meta Immobiliare - Massimo Valore Certificato', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/962490064.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/962490062.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/28201/meta-san-donato/', 'address': 'Via Alfonsine 34 20097 - San Donato Milanese', 'bannerImage': 'https://pic.im-cdn.it/image/854995656/xs-c.jpg', 'externalId': None, 'timeContract': 9, 'paid': False}, {'id': 3986, 'displayName': 'TREC s.a.s', 'imageUrls': {'large': 'https://pic.im-cdn.it/imagenoresize/1040856624.jpg', 'small': 'https://pic.im-cdn.it/imagenoresize/1040856622.jpg'}, 'agencyUrl': 'https://www.immobiliare.it/agenzie-immobiliari/3986/tre-c/', 'address': 'Via Negroli 49 20133 - Milano', 'bannerImage': 'https://maps.im-cdn.it/static?center=45.467200,9.232320&zoom=15&size=400x230&markers=45.467200,9.232320', 'externalId': None, 'timeContract': 1, 'paid': False}], 'searchAgencyUrl': 'http://www.immobiliare.it/agenzie-immobiliari/milano/?idMZona[]=10294'}
or with selenium to mimic a browser and work on the rendered driver.page_source.
Example
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
url = 'https://www.immobiliare.it/vendita-case/milano/forlanini/?criterio=dataModifica&ordine=desc'
driver.get(url)
soup = BeautifulSoup(driver.page_source)
data = []
for e in soup.select('li.in-realEstateResults__item'):
data.append({
'title':e.a.get('title'),
'imgUrls':[i.get('src') for i in e.select('.nd-list__item img')],
'imgMapInfo': e.select_one('[alt="mappa"]').get('src') if e.select_one('[alt="mappa"]') else None
})
data
Output
[{'title': 'Bilocale buono stato, primo piano, Viale Ungheria - Mecenate, Milano', 'imgUrls': ['https://pwm.im-cdn.it/image/1261450576/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261450580/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261702222/xxs-c.jpg', 'https://maps.im-cdn.it/static?zoom=15&size=360x270&language=it&style=feature%3Aroad%7Celement%3Alabels%7Cvisibility%3Aoff&sensor=false&markers=icon%3Ahttps%3A%2F%2Fs1.immobiliare.it%2F_next%2Fstatic%2Fmedia%2Fmap-marker.27fc2b6f.png%7C45.4565%2C9.2427&center=45.4565%2C9.2427', 'https://pic.im-cdn.it/imagenoresize/875151762.jpg'], 'imgMapInfo': 'https://maps.im-cdn.it/static?zoom=15&size=360x270&language=it&style=feature%3Aroad%7Celement%3Alabels%7Cvisibility%3Aoff&sensor=false&markers=icon%3Ahttps%3A%2F%2Fs1.immobiliare.it%2F_next%2Fstatic%2Fmedia%2Fmap-marker.27fc2b6f.png%7C45.4565%2C9.2427&center=45.4565%2C9.2427'}, {'title': 'Bilocale via Romualdo Bonfadini 82, Viale Ungheria - Mecenate, Milano', 'imgUrls': ['https://pwm.im-cdn.it/image/1261689706/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689762/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689770/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689736/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689806/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689780/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689794/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689744/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689718/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689728/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689628/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689636/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689752/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689674/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689694/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689680/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689690/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689670/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689652/xxs-c.jpg', 'https://pwm.im-cdn.it/image/1261689816/xxs-c.jpg', 'https://maps.im-cdn.it/static?zoom=15&size=360x270&language=it&style=feature%3Aroad%7Celement%3Alabels%7Cvisibility%3Aoff&sensor=false&markers=icon%3Ahttps%3A%2F%2Fs1.immobiliare.it%2F_next%2Fstatic%2Fmedia%2Fmap-marker.27fc2b6f.png%7C45.4442%2C9.2417&center=45.4442%2C9.2417', 'https://pic.im-cdn.it/imagenoresize/949757836.jpg'], 'imgMapInfo': 'https://maps.im-cdn.it/static?zoom=15&size=360x270&language=it&style=feature%3Aroad%7Celement%3Alabels%7Cvisibility%3Aoff&sensor=false&markers=icon%3Ahttps%3A%2F%2Fs1.immobiliare.it%2F_next%2Fstatic%2Fmedia%2Fmap-marker.27fc2b6f.png%7C45.4442%2C9.2417&center=45.4442%2C9.2417'}, {'title': 'Appartamento via Oreste Salomone, Viale Ungheria - Mecenate, Milano', 'imgUrls': ['https://pwm.im-cdn.it/image/1256189648/xxs-c.jpg', 'https://pic.im-cdn.it/imagenoresize/994952108.jpg'], 'imgMapInfo': None},...]

from bs4 import BeautifulSoup
html = """
<div class="in-mediaContent">
<div class="nd-figure in-photo in-Card__photo--big">
<div class="nd-figure__image nd-ratio">
::before
<div class="nd-slideshow nd-slideshow--small">
<div class="nd-slideshow__content>
</div>
<div class="nd-slideshow__item
</div>
<div class="nd-slideshow__item
</div>
<div class="nd-slideshow__desired_item
<img src =”desired link”>
</div>
</div>
</div>
</div>
</div>"""
soup = BeautifulSoup(html, 'html.parser')
r = soup.select('div[class*="nd-slideshow"]')
print(r)
in result html after ::before
[<div class="nd-slideshow nd-slideshow--small">
<div <="" class="nd-slideshow__content> </div> <div class=" div="" nd-slideshow__item="">
<div <img="" class="nd-slideshow__item </div> <div class=" link”="" nd-slideshow__desired_item="" src="”desired">
</div>
</div>
</div>, <div <="" class="nd-slideshow__content> </div> <div class=" div="" nd-slideshow__item="">
<div <img="" class="nd-slideshow__item </div> <div class=" link”="" nd-slideshow__desired_item="" src="”desired">
</div>
</div>, <div <img="" class="nd-slideshow__item </div> <div class=" link”="" nd-slideshow__desired_item="" src="”desired">
</div>]

Based on your screenshot, I searched the div element that has the "nd-slideshow__item in-realEstateListCard__mapInfo" class and then I could get the image inside the "div" element.
With this idea, I've modified your code as follows:
import requests
from bs4 import BeautifulSoup
url = "https://www.immobiliare.it/vendita-case/milano/forlanini/?criterio=dataModifica&ordine=desc"
page = requests.get(url)
soup = BeautifulSoup(page.content, "lxml")
# The image you want is inside a img HTML element which is contained inside a "div" element:
div_element = soup.find_all("div", class_="nd-slideshow__item in-realEstateListCard__mapInfo")
# Print the "src" value of the img HTML element found on the div
print(div_element[0].find("img")["src"])
And this is the result I got:
https://maps.im-cdn.it/static?zoom=15&size=360x270&language=it&style=feature%3Aroad%7Celement%3Alabels%7Cvisibility%3Aoff&sensor=false&markers=icon%3Ahttps%3A%2F%2Fs1.immobiliare.it%2F_next%2Fstatic%2Fmedia%2Fmap-marker.27fc2b6f.png%7C45.4565%2C9.2427&center=45.4565%2C9.2427

Related

How to Transform data in a nested json?

I have the following data, and when I used json_flatten i was able to extract most of the data except for deliveryMethod.items and languages.items.
I also tried to use pd.json_normalize(a, record_path= 'deliveryMethod.items') but it doesn't seem to be working.
a = {'ID': '1', 'Name': 'ABC', 'Center': 'Center For Education', 'providerNameAr': 'ABC', 'city': {'id': 1, 'cityEn': 'LA', 'regionId': 0, 'region': None}, 'cityName': None, 'LevelNumber': 'ABCD', 'activityStartDate': '09/01/2020', 'activityEndDate': '09/02/2020', 'activityType': {'lookUpId': 2, 'lookUpEn': 'Course', 'code': None, 'parent': None, 'hasParent': False}, 'deliveryMethod': {'items': [{'lookUpId': 2, 'lookUpEn': 'online' 'code': None, 'parent': None, 'hasParent': False}]}, 'languages': {'items': [{'lookUpId': 1, 'lookUpEn': 'English', 'code': None, 'parent': None, 'hasParent': False}]}, 'activityCategory': {'lookUpId': 1, 'lookUpEn': 'Regular', 'code': None, 'parent': None, 'hasParent': False}, 'address': 'LA', 'phoneNumber': '-11111', 'emailAddress': 'ABCS#Gmail.com', 'isAllSpeciality': True, 'requestId': 23, 'parentActivityId': None, 'sppData': None}

Iterating over JSON data and printing. (or creating Pandas DataFrame from JSON file)

I’m trying to use Python print specific values from a JSON file that I pulled from an API. From what I understand, I am pulling it as a JSON file that has a list of dictionaries of players, with a nested dictionary for each player containing their data (i.e. name, team, etc.).
I’m running into issues printing the values within the JSON file, as each character is printing on a separate line.
The end result I am trying to get to is a Pandas DataFrame containing all the values from the JSON file, but I can’t even seem to iterate through the JSON file correctly.
Here is my code:
url = "https://api-football-v1.p.rapidapi.com/v3/players"
querystring = {"league":"39","season":"2020", "page":"2"}
headers = {
"X-RapidAPI-Host": "api-football-v1.p.rapidapi.com",
"X-RapidAPI-Key": "xxxxxkeyxxxxx"
}
response = requests.request("GET", url, headers=headers, params=querystring).json()
response_dump = json.dumps(response)
for item in response_dump:
for player_item in item:
print(player_item)
This is the output when I print the JSON response (first two items):
{'get': 'players', 'parameters': {'league': '39', 'page': '2', 'season': '2020'}, 'errors': [], 'results': 20, 'paging': {'current': 2, 'total': 37}, 'response': [{'player': {'id': 301, 'name': 'Benjamin Luke Woodburn', 'firstname': 'Benjamin Luke', 'lastname': 'Woodburn', 'age': 23, 'birth': {'date': '1999-10-15', 'place': 'Nottingham', 'country': 'England'}, 'nationality': 'Wales', 'height': '174 cm', 'weight': '72 kg', 'injured': False, 'photo': 'https://media.api-sports.io/football/players/301.png'}, 'statistics': [{'team': {'id': 40, 'name': 'Liverpool', 'logo': 'https://media.api-sports.io/football/teams/40.png'}, 'league': {'id': 39, 'name': 'Premier League', 'country': 'England', 'logo': 'https://media.api-sports.io/football/leagues/39.png', 'flag': 'https://media.api-sports.io/flags/gb.svg', 'season': 2020}, 'games': {'appearences': 0, 'lineups': 0, 'minutes': 0, 'number': None, 'position': 'Attacker', 'rating': None, 'captain': False}, 'substitutes': {'in': 0, 'out': 0, 'bench': 3}, 'shots': {'total': None, 'on': None}, 'goals': {'total': 0, 'conceded': 0, 'assists': None, 'saves': None}, 'passes': {'total': None, 'key': None, 'accuracy': None}, 'tackles': {'total': None, 'blocks': None, 'interceptions': None}, 'duels': {'total': None, 'won': None}, 'dribbles': {'attempts': None, 'success': None, 'past': None}, 'fouls': {'drawn': None, 'committed': None}, 'cards': {'yellow': 0, 'yellowred': 0, 'red': 0}, 'penalty': {'won': None, 'commited': None, 'scored': 0, 'missed': 0, 'saved': None}}]}, {'player': {'id': 518, 'name': 'Meritan Shabani', 'firstname': 'Meritan', 'lastname': 'Shabani', 'age': 23, 'birth': {'date': '1999-03-15', 'place': 'München', 'country': 'Germany'}, 'nationality': 'Germany', 'height': '185 cm', 'weight': '78 kg', 'injured': False, 'photo': 'https://media.api-sports.io/football/players/518.png'}, 'statistics': [{'team': {'id': 39, 'name': 'Wolves', 'logo': 'https://media.api-sports.io/football/teams/39.png'}, 'league': {'id': 39, 'name': 'Premier League', 'country': 'England', 'logo': 'https://media.api-sports.io/football/leagues/39.png', 'flag': 'https://media.api-sports.io/flags/gb.svg', 'season': 2020}, 'games': {'appearences': 0, 'lineups': 0, 'minutes': 0, 'number': None, 'position': 'Midfielder', 'rating': None, 'captain': False}, 'substitutes': {'in': 0, 'out': 0, 'bench': 3}, 'shots': {'total': None, 'on': None}, 'goals': {'total': 0, 'conceded': 0, 'assists': None, 'saves': None}, 'passes': {'total': None, 'key': None, 'accuracy': None}, 'tackles': {'total': None, 'blocks': None, 'interceptions': None}, 'duels': {'total': None, 'won': None}, 'dribbles': {'attempts': None, 'success': None, 'past': None}, 'fouls': {'drawn': None, 'committed': None}, 'cards': {'yellow': 0, 'yellowred': 0, 'red': 0}, 'penalty': {'won': None, 'commited': None, 'scored': 0, 'missed': 0, 'saved': None}}]},
This is the data type of each layer of the JSON file, from when I iterated through it with a For loop:
print(type(response)) <class 'dict'>
print(type(response_dump)) <class 'str'>
print(type(item)) <class 'str'>
print(type(player_item)) <class 'str'>
You do not have to json.dumps() in my opinion, just use the JSON from response to iterate:
for player in response['response']:
print(player)
{'player': {'id': 301, 'name': 'Benjamin Luke Woodburn', 'firstname': 'Benjamin Luke', 'lastname': 'Woodburn', 'age': 23, 'birth': {'date': '1999-10-15', 'place': 'Nottingham', 'country': 'England'}, 'nationality': 'Wales', 'height': '174 cm', 'weight': '72 kg', 'injured': False, 'photo': 'https://media.api-sports.io/football/players/301.png'}, 'statistics': [{'team': {'id': 40, 'name': 'Liverpool', 'logo': 'https://media.api-sports.io/football/teams/40.png'}, 'league': {'id': 39, 'name': 'Premier League', 'country': 'England', 'logo': 'https://media.api-sports.io/football/leagues/39.png', 'flag': 'https://media.api-sports.io/flags/gb.svg', 'season': 2020}, 'games': {'appearences': 0, 'lineups': 0, 'minutes': 0, 'number': None, 'position': 'Attacker', 'rating': None, 'captain': False}, 'substitutes': {'in': 0, 'out': 0, 'bench': 3}, 'shots': {'total': None, 'on': None}, 'goals': {'total': 0, 'conceded': 0, 'assists': None, 'saves': None}, 'passes': {'total': None, 'key': None, 'accuracy': None}, 'tackles': {'total': None, 'blocks': None, 'interceptions': None}, 'duels': {'total': None, 'won': None}, 'dribbles': {'attempts': None, 'success': None, 'past': None}, 'fouls': {'drawn': None, 'committed': None}, 'cards': {'yellow': 0, 'yellowred': 0, 'red': 0}, 'penalty': {'won': None, 'commited': None, 'scored': 0, 'missed': 0, 'saved': None}}]}
{'player': {'id': 518, 'name': 'Meritan Shabani', 'firstname': 'Meritan', 'lastname': 'Shabani', 'age': 23, 'birth': {'date': '1999-03-15', 'place': 'München', 'country': 'Germany'}, 'nationality': 'Germany', 'height': '185 cm', 'weight': '78 kg', 'injured': False, 'photo': 'https://media.api-sports.io/football/players/518.png'}, 'statistics': [{'team': {'id': 39, 'name': 'Wolves', 'logo': 'https://media.api-sports.io/football/teams/39.png'}, 'league': {'id': 39, 'name': 'Premier League', 'country': 'England', 'logo': 'https://media.api-sports.io/football/leagues/39.png', 'flag': 'https://media.api-sports.io/flags/gb.svg', 'season': 2020}, 'games': {'appearences': 0, 'lineups': 0, 'minutes': 0, 'number': None, 'position': 'Midfielder', 'rating': None, 'captain': False}, 'substitutes': {'in': 0, 'out': 0, 'bench': 3}, 'shots': {'total': None, 'on': None}, 'goals': {'total': 0, 'conceded': 0, 'assists': None, 'saves': None}, 'passes': {'total': None, 'key': None, 'accuracy': None}, 'tackles': {'total': None, 'blocks': None, 'interceptions': None}, 'duels': {'total': None, 'won': None}, 'dribbles': {'attempts': None, 'success': None, 'past': None}, 'fouls': {'drawn': None, 'committed': None}, 'cards': {'yellow': 0, 'yellowred': 0, 'red': 0}, 'penalty': {'won': None, 'commited': None, 'scored': 0, 'missed': 0, 'saved': None}}]}
or
for player in response['response']:
print(player['player'])
{'id': 301, 'name': 'Benjamin Luke Woodburn', 'firstname': 'Benjamin Luke', 'lastname': 'Woodburn', 'age': 23, 'birth': {'date': '1999-10-15', 'place': 'Nottingham', 'country': 'England'}, 'nationality': 'Wales', 'height': '174 cm', 'weight': '72 kg', 'injured': False, 'photo': 'https://media.api-sports.io/football/players/301.png'}
{'id': 518, 'name': 'Meritan Shabani', 'firstname': 'Meritan', 'lastname': 'Shabani', 'age': 23, 'birth': {'date': '1999-03-15', 'place': 'München', 'country': 'Germany'}, 'nationality': 'Germany', 'height': '185 cm', 'weight': '78 kg', 'injured': False, 'photo': 'https://media.api-sports.io/football/players/518.png'}
To get a DataFrame simply call pd.json_normalize() - Cause your question is not that clear I am not sure wiche information is needed and how to displayed. This is predestinated to ask a new question with exact that focus.:
pd.json_normalize(response['response'])
EDIT
Based on your comment and improvment:
pd.concat([pd.json_normalize(response,['response'])\
,pd.json_normalize(response,['response','statistics'])], axis=1)\
.drop(['statistics'], axis=1)
player.id
player.name
player.firstname
player.lastname
player.age
player.birth.date
player.birth.place
player.birth.country
player.nationality
player.height
player.weight
player.injured
player.photo
team.id
team.name
team.logo
league.id
league.name
league.country
league.logo
league.flag
league.season
games.appearences
games.lineups
games.minutes
games.number
games.position
games.rating
games.captain
substitutes.in
substitutes.out
substitutes.bench
shots.total
shots.on
goals.total
goals.conceded
goals.assists
goals.saves
passes.total
passes.key
passes.accuracy
tackles.total
tackles.blocks
tackles.interceptions
duels.total
duels.won
dribbles.attempts
dribbles.success
dribbles.past
fouls.drawn
fouls.committed
cards.yellow
cards.yellowred
cards.red
penalty.won
penalty.commited
penalty.scored
penalty.missed
penalty.saved
0
301
Benjamin Luke Woodburn
Benjamin Luke
Woodburn
23
1999-10-15
Nottingham
England
Wales
174 cm
72 kg
False
https://media.api-sports.io/football/players/301.png
40
Liverpool
https://media.api-sports.io/football/teams/40.png
39
Premier League
England
https://media.api-sports.io/football/leagues/39.png
https://media.api-sports.io/flags/gb.svg
2020
0
0
0
Attacker
False
0
0
3
0
0
0
0
0
0
0
1
518
Meritan Shabani
Meritan
Shabani
23
1999-03-15
München
Germany
Germany
185 cm
78 kg
False
https://media.api-sports.io/football/players/518.png
39
Wolves
https://media.api-sports.io/football/teams/39.png
39
Premier League
England
https://media.api-sports.io/football/leagues/39.png
https://media.api-sports.io/flags/gb.svg
2020
0
0
0
Midfielder
False
0
0
3
0
0
0
0
0
0
0

Convert nested dictionary, list, and dictionary into a pandas data frame in python

So, I am trying to work with a rest API, and it is giving me the following data:
{'sports': [{'id': '20',
'uid': 's:20',
'name': 'Football',
'slug': 'football',
'leagues': [{'id': '28',
'uid': 's:20~l:28',
'name': 'National Football League',
'abbreviation': 'NFL',
'shortName': 'NFL',
'slug': 'nfl',
'teams': [{'team': {'id': '22',
'uid': 's:20~l:28~t:22',
'slug': 'arizona-cardinals',
'location': 'Arizona',
'name': 'Cardinals',
'nickname': 'Cardinals',
'abbreviation': 'ARI',
'displayName': 'Arizona Cardinals',
'shortDisplayName': 'Cardinals',
'color': 'A40227',
'alternateColor': '000000',
'isActive': True,
'isAllStar': False,
'logos': [{'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/ari.png',
'width': 500,
'height': 500,
'alt': '',
'rel': ['full', 'default'],
'lastUpdated': '2018-06-05T12:11Z'},
{'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/ari.png',
'width': 500,
'height': 500,
'alt': '',
'rel': ['full', 'dark'],
'lastUpdated': '2018-06-05T12:11Z'},
{'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/scoreboard/ari.png',
'width': 500,
'height': 500,
'alt': '',
'rel': ['full', 'scoreboard'],
'lastUpdated': '2018-06-05T12:11Z'},
...
I'm just interested in the teams data. However, I try to slice the pie, I'm having trouble extracting the desired information into the dataframe properly.
Here is my code:
url = 'http://site.api.espn.com/apis/site/v2/sports/football/nfl/teams'
r = requests.get(url)
teams_json = r.json()
nfl = []
for teams in teams_json.items():
for x in teams:
for row in x:
print(row['teams'])
I keep getting errors.
Any assistance is greatly appreciated.
I'd suggest looking into how to navigate lists and dictionaries in python (that's all that json files are). It's just a matter of knowing the path, or how to iterate through those.
To get into a dataframe, pandas has a nice .json_normalize() method. I'm not sure what data you want exactly, as the root teams key data is also nested. So depending what you are after, you may need to do a little extra work to extract what you want. But this is the general dataframe for teams.
import requests
import pandas as pd
url = 'http://site.api.espn.com/apis/site/v2/sports/football/nfl/teams'
jsonData = requests.get(url).json()
teams_json = jsonData['sports'][0]['leagues'][0]['teams']
df = pd.json_normalize(teams_json)
Output:
print(df.head().to_string())
team.id team.uid team.slug team.location team.name team.nickname team.abbreviation team.displayName team.shortDisplayName team.color team.alternateColor team.isActive team.isAllStar team.logos team.record.items team.links
0 22 s:20~l:28~t:22 arizona-cardinals Arizona Cardinals Cardinals ARI Arizona Cardinals Cardinals A40227 000000 True False [{'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/ari.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'default'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/ari.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'dark'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/scoreboard/ari.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'scoreboard'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/scoreboard/ari.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'scoreboard', 'dark'], 'lastUpdated': '2018-06-05T12:11Z'}] [{'summary': '11-6', 'stats': [{'name': 'playoffSeed', 'value': 5.0}, {'name': 'wins', 'value': 11.0}, {'name': 'losses', 'value': 6.0}, {'name': 'winPercent', 'value': 0.6470588445663452}, {'name': 'gamesBehind', 'value': 0.0}, {'name': 'ties', 'value': 0.0}, {'name': 'OTWins', 'value': 0.0}, {'name': 'OTLosses', 'value': 0.0}, {'name': 'gamesPlayed', 'value': 17.0}, {'name': 'pointsFor', 'value': 449.0}, {'name': 'pointsAgainst', 'value': 366.0}, {'name': 'avgPointsFor', 'value': 26.41176414489746}, {'name': 'avgPointsAgainst', 'value': 21.52941131591797}, {'name': 'points', 'value': 2.5}, {'name': 'differential', 'value': 83.0}, {'name': 'streak', 'value': -1.0}, {'name': 'clincher', 'value': 0.0}, {'name': 'divisionWinPercent', 'value': 0.6666666865348816}, {'name': 'leagueWinPercent', 'value': 0.5833333134651184}, {'name': 'divisionRecord', 'value': 0.0}, {'name': 'divisionWins', 'value': 4.0}, {'name': 'divisionTies', 'value': 0.0}, {'name': 'divisionLosses', 'value': 2.0}]}] [{'language': 'en-US', 'rel': ['clubhouse', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/_/name/ari/arizona-cardinals', 'text': 'Clubhouse', 'shortText': 'Clubhouse', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['roster', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/team/roster/_/name/ari/arizona-cardinals', 'text': 'Roster', 'shortText': 'Roster', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['stats', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/team/stats/_/name/ari/arizona-cardinals', 'text': 'Statistics', 'shortText': 'Statistics', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['schedule', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/schedule/_/name/ari', 'text': 'Schedule', 'shortText': 'Schedule', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['photos', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/photos/_/name/ari', 'text': 'photos', 'shortText': 'photos', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['scores', 'sportscenter', 'app', 'team'], 'href': 'sportscenter://x-callback-url/showClubhouse?uid=s:20~l:28~t:22&section=scores', 'text': 'Scores', 'shortText': 'Scores', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['draftpicks', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/draft/teams/_/name/ari/arizona-cardinals', 'text': 'Draft Picks', 'shortText': 'Draft Picks', 'isExternal': False, 'isPremium': True}, {'language': 'en-US', 'rel': ['transactions', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/transactions/_/name/ari', 'text': 'Transactions', 'shortText': 'Transactions', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['injuries', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/injuries/_/name/ari', 'text': 'Injuries', 'shortText': 'Injuries', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['depthchart', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/depth/_/name/ari', 'text': 'Depth Chart', 'shortText': 'Depth Chart', 'isExternal': False, 'isPremium': False}, {'language': 'en', 'rel': ['tickets', 'desktop', 'team'], 'href': 'https://www.vividseats.com/nfl-football/arizona-cardinals-tickets.html?wsUser=717', 'text': 'Tickets', 'isExternal': True, 'isPremium': False}]
1 1 s:20~l:28~t:1 atlanta-falcons Atlanta Falcons Falcons ATL Atlanta Falcons Falcons 000000 000000 True False [{'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/atl.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'default'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/atl.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'dark'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/scoreboard/atl.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'scoreboard'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/scoreboard/atl.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'scoreboard', 'dark'], 'lastUpdated': '2018-06-05T12:11Z'}] [{'summary': '7-10', 'stats': [{'name': 'playoffSeed', 'value': 12.0}, {'name': 'wins', 'value': 7.0}, {'name': 'losses', 'value': 10.0}, {'name': 'winPercent', 'value': 0.4117647111415863}, {'name': 'gamesBehind', 'value': 0.0}, {'name': 'ties', 'value': 0.0}, {'name': 'OTWins', 'value': 0.0}, {'name': 'OTLosses', 'value': 0.0}, {'name': 'gamesPlayed', 'value': 17.0}, {'name': 'pointsFor', 'value': 313.0}, {'name': 'pointsAgainst', 'value': 459.0}, {'name': 'avgPointsFor', 'value': 18.41176414489746}, {'name': 'avgPointsAgainst', 'value': 27.0}, {'name': 'points', 'value': -1.5}, {'name': 'differential', 'value': -146.0}, {'name': 'streak', 'value': -2.0}, {'name': 'clincher', 'value': 0.0}, {'name': 'divisionWinPercent', 'value': 0.3333333432674408}, {'name': 'leagueWinPercent', 'value': 0.3333333432674408}, {'name': 'divisionRecord', 'value': 0.0}, {'name': 'divisionWins', 'value': 2.0}, {'name': 'divisionTies', 'value': 0.0}, {'name': 'divisionLosses', 'value': 4.0}]}] [{'language': 'en-US', 'rel': ['clubhouse', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/_/name/atl/atlanta-falcons', 'text': 'Clubhouse', 'shortText': 'Clubhouse', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['roster', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/team/roster/_/name/atl/atlanta-falcons', 'text': 'Roster', 'shortText': 'Roster', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['stats', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/team/stats/_/name/atl/atlanta-falcons', 'text': 'Statistics', 'shortText': 'Statistics', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['schedule', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/schedule/_/name/atl', 'text': 'Schedule', 'shortText': 'Schedule', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['photos', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/photos/_/name/atl', 'text': 'photos', 'shortText': 'photos', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['scores', 'sportscenter', 'app', 'team'], 'href': 'sportscenter://x-callback-url/showClubhouse?uid=s:20~l:28~t:1&section=scores', 'text': 'Scores', 'shortText': 'Scores', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['draftpicks', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/draft/teams/_/name/atl/atlanta-falcons', 'text': 'Draft Picks', 'shortText': 'Draft Picks', 'isExternal': False, 'isPremium': True}, {'language': 'en-US', 'rel': ['transactions', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/transactions/_/name/atl', 'text': 'Transactions', 'shortText': 'Transactions', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['injuries', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/injuries/_/name/atl', 'text': 'Injuries', 'shortText': 'Injuries', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['depthchart', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/depth/_/name/atl', 'text': 'Depth Chart', 'shortText': 'Depth Chart', 'isExternal': False, 'isPremium': False}, {'language': 'en', 'rel': ['tickets', 'desktop', 'team'], 'href': 'https://www.vividseats.com/nfl-football/atlanta-falcons-tickets.html?wsUser=717', 'text': 'Tickets', 'isExternal': True, 'isPremium': False}]
2 2 s:20~l:28~t:2 buffalo-bills Buffalo Bills Bills BUF Buffalo Bills Bills 04407F c60c30 True False [{'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/buf.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'default'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/buf.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'dark'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/scoreboard/buf.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'scoreboard'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/scoreboard/buf.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'scoreboard', 'dark'], 'lastUpdated': '2018-06-05T12:11Z'}] [{'summary': '11-6', 'stats': [{'name': 'playoffSeed', 'value': 3.0}, {'name': 'wins', 'value': 11.0}, {'name': 'losses', 'value': 6.0}, {'name': 'winPercent', 'value': 0.6470588445663452}, {'name': 'gamesBehind', 'value': 0.0}, {'name': 'ties', 'value': 0.0}, {'name': 'OTWins', 'value': 0.0}, {'name': 'OTLosses', 'value': 1.0}, {'name': 'gamesPlayed', 'value': 17.0}, {'name': 'pointsFor', 'value': 483.0}, {'name': 'pointsAgainst', 'value': 289.0}, {'name': 'avgPointsFor', 'value': 28.41176414489746}, {'name': 'avgPointsAgainst', 'value': 17.0}, {'name': 'points', 'value': 2.5}, {'name': 'differential', 'value': 194.0}, {'name': 'streak', 'value': 4.0}, {'name': 'clincher', 'value': 0.0}, {'name': 'divisionWinPercent', 'value': 0.8333333134651184}, {'name': 'leagueWinPercent', 'value': 0.5833333134651184}, {'name': 'divisionRecord', 'value': 0.0}, {'name': 'divisionWins', 'value': 5.0}, {'name': 'divisionTies', 'value': 0.0}, {'name': 'divisionLosses', 'value': 1.0}]}] [{'language': 'en-US', 'rel': ['clubhouse', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/_/name/buf/buffalo-bills', 'text': 'Clubhouse', 'shortText': 'Clubhouse', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['roster', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/team/roster/_/name/buf/buffalo-bills', 'text': 'Roster', 'shortText': 'Roster', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['stats', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/team/stats/_/name/buf/buffalo-bills', 'text': 'Statistics', 'shortText': 'Statistics', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['schedule', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/schedule/_/name/buf', 'text': 'Schedule', 'shortText': 'Schedule', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['photos', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/photos/_/name/buf', 'text': 'photos', 'shortText': 'photos', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['scores', 'sportscenter', 'app', 'team'], 'href': 'sportscenter://x-callback-url/showClubhouse?uid=s:20~l:28~t:2&section=scores', 'text': 'Scores', 'shortText': 'Scores', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['draftpicks', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/draft/teams/_/name/buf/buffalo-bills', 'text': 'Draft Picks', 'shortText': 'Draft Picks', 'isExternal': False, 'isPremium': True}, {'language': 'en-US', 'rel': ['transactions', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/transactions/_/name/buf', 'text': 'Transactions', 'shortText': 'Transactions', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['injuries', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/injuries/_/name/buf', 'text': 'Injuries', 'shortText': 'Injuries', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['depthchart', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/depth/_/name/buf', 'text': 'Depth Chart', 'shortText': 'Depth Chart', 'isExternal': False, 'isPremium': False}, {'language': 'en', 'rel': ['tickets', 'desktop', 'team'], 'href': 'https://www.vividseats.com/nfl-football/buffalo-bills-tickets.html?wsUser=717', 'text': 'Tickets', 'isExternal': True, 'isPremium': False}]
3 3 s:20~l:28~t:3 chicago-bears Chicago Bears Bears CHI Chicago Bears Bears 152644 0b162a True False [{'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/chi.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'default'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/chi.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'dark'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/scoreboard/chi.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'scoreboard'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/scoreboard/chi.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'scoreboard', 'dark'], 'lastUpdated': '2018-06-05T12:11Z'}] [{'summary': '6-11', 'stats': [{'name': 'playoffSeed', 'value': 13.0}, {'name': 'wins', 'value': 6.0}, {'name': 'losses', 'value': 11.0}, {'name': 'winPercent', 'value': 0.3529411852359772}, {'name': 'gamesBehind', 'value': 0.0}, {'name': 'ties', 'value': 0.0}, {'name': 'OTWins', 'value': 0.0}, {'name': 'OTLosses', 'value': 0.0}, {'name': 'gamesPlayed', 'value': 17.0}, {'name': 'pointsFor', 'value': 311.0}, {'name': 'pointsAgainst', 'value': 407.0}, {'name': 'avgPointsFor', 'value': 18.294116973876953}, {'name': 'avgPointsAgainst', 'value': 23.941177368164062}, {'name': 'points', 'value': -2.5}, {'name': 'differential', 'value': -96.0}, {'name': 'streak', 'value': -1.0}, {'name': 'clincher', 'value': 0.0}, {'name': 'divisionWinPercent', 'value': 0.3333333432674408}, {'name': 'leagueWinPercent', 'value': 0.3333333432674408}, {'name': 'divisionRecord', 'value': 0.0}, {'name': 'divisionWins', 'value': 2.0}, {'name': 'divisionTies', 'value': 0.0}, {'name': 'divisionLosses', 'value': 4.0}]}] [{'language': 'en-US', 'rel': ['clubhouse', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/_/name/chi/chicago-bears', 'text': 'Clubhouse', 'shortText': 'Clubhouse', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['roster', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/team/roster/_/name/chi/chicago-bears', 'text': 'Roster', 'shortText': 'Roster', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['stats', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/team/stats/_/name/chi/chicago-bears', 'text': 'Statistics', 'shortText': 'Statistics', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['schedule', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/schedule/_/name/chi', 'text': 'Schedule', 'shortText': 'Schedule', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['photos', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/photos/_/name/chi', 'text': 'photos', 'shortText': 'photos', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['scores', 'sportscenter', 'app', 'team'], 'href': 'sportscenter://x-callback-url/showClubhouse?uid=s:20~l:28~t:3&section=scores', 'text': 'Scores', 'shortText': 'Scores', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['draftpicks', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/draft/teams/_/name/chi/chicago-bears', 'text': 'Draft Picks', 'shortText': 'Draft Picks', 'isExternal': False, 'isPremium': True}, {'language': 'en-US', 'rel': ['transactions', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/transactions/_/name/chi', 'text': 'Transactions', 'shortText': 'Transactions', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['injuries', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/injuries/_/name/chi', 'text': 'Injuries', 'shortText': 'Injuries', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['depthchart', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/depth/_/name/chi', 'text': 'Depth Chart', 'shortText': 'Depth Chart', 'isExternal': False, 'isPremium': False}, {'language': 'en', 'rel': ['tickets', 'desktop', 'team'], 'href': 'https://www.vividseats.com/nfl-football/chicago-bears-tickets.html?wsUser=717', 'text': 'Tickets', 'isExternal': True, 'isPremium': False}]
4 4 s:20~l:28~t:4 cincinnati-bengals Cincinnati Bengals Bengals CIN Cincinnati Bengals Bengals FF2700 000000 True False [{'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/cin.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'default'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/cin.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'dark'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500/scoreboard/cin.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'scoreboard'], 'lastUpdated': '2018-06-05T12:11Z'}, {'href': 'https://a.espncdn.com/i/teamlogos/nfl/500-dark/scoreboard/cin.png', 'width': 500, 'height': 500, 'alt': '', 'rel': ['full', 'scoreboard', 'dark'], 'lastUpdated': '2018-06-05T12:11Z'}] [{'summary': '10-7', 'stats': [{'name': 'playoffSeed', 'value': 4.0}, {'name': 'wins', 'value': 10.0}, {'name': 'losses', 'value': 7.0}, {'name': 'winPercent', 'value': 0.5882353186607361}, {'name': 'gamesBehind', 'value': 0.0}, {'name': 'ties', 'value': 0.0}, {'name': 'OTWins', 'value': 1.0}, {'name': 'OTLosses', 'value': 2.0}, {'name': 'gamesPlayed', 'value': 17.0}, {'name': 'pointsFor', 'value': 460.0}, {'name': 'pointsAgainst', 'value': 376.0}, {'name': 'avgPointsFor', 'value': 27.058822631835938}, {'name': 'avgPointsAgainst', 'value': 22.117647171020508}, {'name': 'points', 'value': 1.5}, {'name': 'differential', 'value': 84.0}, {'name': 'streak', 'value': -1.0}, {'name': 'clincher', 'value': 0.0}, {'name': 'divisionWinPercent', 'value': 0.6666666865348816}, {'name': 'leagueWinPercent', 'value': 0.6666666865348816}, {'name': 'divisionRecord', 'value': 0.0}, {'name': 'divisionWins', 'value': 4.0}, {'name': 'divisionTies', 'value': 0.0}, {'name': 'divisionLosses', 'value': 2.0}]}] [{'language': 'en-US', 'rel': ['clubhouse', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/_/name/cin/cincinnati-bengals', 'text': 'Clubhouse', 'shortText': 'Clubhouse', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['roster', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/team/roster/_/name/cin/cincinnati-bengals', 'text': 'Roster', 'shortText': 'Roster', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['stats', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/team/stats/_/name/cin/cincinnati-bengals', 'text': 'Statistics', 'shortText': 'Statistics', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['schedule', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/schedule/_/name/cin', 'text': 'Schedule', 'shortText': 'Schedule', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['photos', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/photos/_/name/cin', 'text': 'photos', 'shortText': 'photos', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['scores', 'sportscenter', 'app', 'team'], 'href': 'sportscenter://x-callback-url/showClubhouse?uid=s:20~l:28~t:4&section=scores', 'text': 'Scores', 'shortText': 'Scores', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['draftpicks', 'desktop', 'team'], 'href': 'http://www.espn.com/nfl/draft/teams/_/name/cin/cincinnati-bengals', 'text': 'Draft Picks', 'shortText': 'Draft Picks', 'isExternal': False, 'isPremium': True}, {'language': 'en-US', 'rel': ['transactions', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/transactions/_/name/cin', 'text': 'Transactions', 'shortText': 'Transactions', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['injuries', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/injuries/_/name/cin', 'text': 'Injuries', 'shortText': 'Injuries', 'isExternal': False, 'isPremium': False}, {'language': 'en-US', 'rel': ['depthchart', 'desktop', 'team'], 'href': 'https://www.espn.com/nfl/team/depth/_/name/cin', 'text': 'Depth Chart', 'shortText': 'Depth Chart', 'isExternal': False, 'isPremium': False}, {'language': 'en', 'rel': ['tickets', 'desktop', 'team'], 'href': 'https://www.vividseats.com/nfl-football/cincinnati-bengals-tickets.html?wsUser=717', 'text': 'Tickets', 'isExternal': True, 'isPremium': False}]
...
[25 rows x 16 columns]
json.items() returns not just the values but the keys also, meaning that the first value in teams is the string "sports" and the second value is what you are looking for, the list. That's what is causing the error.
Edit: You want to do for key, teams in teams_json.items() not for teams in teams_json.items()

how to scrape page inside the result card using BeatifulSoup?

<a href="https://magicpin.in/New-Delhi/Laxmi-Nagar/Restaurant/Bangalore-Ki-Famous-Biriyani/store/317630/?utm_source=search" data-type="around-merchant-card" onclick="sendEvent('web_searchlandingpage', 'click_search_merchant_card');">
<div class="merchant-logo-holder lazyloaded" data-bg="https://lh3.googleusercontent.com/UE8HJOOmV0hZsCbxbwQyha8UNa2ETY3-3zExps_mxUi-nbxVkgEBXo0kxghMHOUp87pge5tqIGrsU9Pu-iJ3h0_IETE=w64" style="background-image: url("https://lh3.googleusercontent.com/UE8HJOOmV0hZsCbxbwQyha8UNa2ETY3-3zExps_mxUi-nbxVkgEBXo0kxghMHOUp87pge5tqIGrsU9Pu-iJ3h0_IETE=w64");" title=", Laxmi Nagar, New Delhi"></div>
</a>
<div class="merchant-name-address">
<a href="https://magicpin.in/New-Delhi/Laxmi-Nagar/Restaurant/Bangalore-Ki-Famous-Biriyani/store/317630/?utm_source=search" data-type="around-merchant-card" onclick="sendEvent('web_searchlandingpage', 'click_search_merchant_card');">
<h3 class="merchant-name">
Bangalore Ki Famous Biriyani
</h3>
<h4 class="merchant-location">
Laxmi Nagar
, New Delhi
</h4>
</a>
<!--
<a href="" data-type="merchant_card_links" data-target="subcategory">
<h5 class="subcategory-tag"> </h5>
</a>
-->
</div>
<div class="rating" style="background-color: #8bcc00; border-color: #8bcc00;">
<img src="https://static.magicpin.com/samara/static/images/merchant/star-white.svg" class="star" onerror="this.onerror=null;this.alt='';recordBrokenImages(this,false,4);">
<span class="rating-value">3.7</span>
</div>
<section class="merchant-details">
<div class="cft-timing">
<article class="detail-heading cft-heading">Average Spent: </article>
<span class="detail-value">₹500</span>
</div>
<div class="merchant-attributes">
<div class="cover-holder">
<a href="https://magicpin.in/New-Delhi/Laxmi-Nagar/Restaurant/Bangalore-Ki-Famous-Biriyani/store/317630/?utm_source=search" data-type="around-merchant-card" onclick="sendEvent('web_searchlandingpage', 'click_search_merchant_card');">
<div class="merchant-cover lazyloaded" data-bg="https://lh3.googleusercontent.com/W70wpIcovlvssmLSpcyub4RjHABennVNRWxznclaxD7PEcZhdJrgygOwn5qJ3XrlYq9Yv90k-w2Ld0lTfaylBxIGmw=w512" style="background-image: url("https://lh3.googleusercontent.com/W70wpIcovlvssmLSpcyub4RjHABennVNRWxznclaxD7PEcZhdJrgygOwn5qJ3XrlYq9Yv90k-w2Ld0lTfaylBxIGmw=w512");" title=", Laxmi Nagar, New Delhi">
</div>
</a>
</div>
<div class="details">
<article class="detail-heading">Highlights: </article>
<span class="detail-value">
<span class="comma-separator">
Magic Weekend 14 16
</span>
<span class="comma-separator">
Mw 20200123
</span>
</span>
</div>
</div>
</section>
<div class="merchant-card-actions">
<div class="action claim-deal-button-react show-mb" data-sku="food_1158872_other" data-merchantname="Bangalore Ki Famous Biriyani" data-dealid="1164110">
<p class="cashback">
CASHBACK<span>upto 10.0% OFF</span>
</p>
</div>
<a class="cashback hide-mb action" target="_blank" href="https://magicpin.in/deal/?dealId=1164110&userId=5290338">
CASHBACK<span>upto 10.0% OFF</span>
</a>
</div>
</div>
page URL - https://magicpin.in/Delhi/search/?dist=10&query=biriyani&rt=3
this page contains some restaurants card now while scrapping the page in the loop I want to go inside the restaurant card and scrape the no. of reviews from inside it, I don't know how to do it I used this code to scrape front page
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = "https://magicpin.in/Delhi/search/?dist=10&query=biriyani&rt=3" # URL of the website
header = {'User-Agent':'Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36'} # Temporary user agent
r = requests.get(url, headers=header)
soup = BeautifulSoup(r.content, 'html.parser')
divs = soup.find_all('div', class_ ="merchant-card-single")
for item in divs:
title = item.find('h3').text.strip() # restaurant name
loc = item.find('h4', class_ ="merchant-location").text.strip() # restaurant location
try: # used this try and except method because some restaurants are unrated and while scrpaping those we would run into an error
rating = item.find('div', class_="rating").text
except:
rating = None
pricce = item.find('div', class_="cft-timing").text.strip() # price for biriyani
biry_del = {
'name': title,
'location': loc,
'rating': rating,
'price': price
}
rest_list.append(biry_del)
I hope you guys understood please ask in comment for any confusion.
Note: Code in your question is not valid, variables are not defined or have typos and there is no expected result - So it is just a hint in the right direction
What happens?
Take a look into your soup, there is no <div> that contains the rating.
How to fix?
Select more specific - Rating is stored in a <span> with class rating-value:
rating = item.find('span', class_="rating-value").text
EDIT
Based on your comment you wanna switch to details and do things there - Just grab the url from <a> and perform another request:
url = item.find('a').get('href')
detailsSoup = BeautifulSoup(requests.get(url).text)
### look into your detailsSoup to find what you are searching for ...
To grab the raiting details it would be better to use the existing api - Extract merchands id from url and grab the json data:
url = item.find('a').get('href')
mid = url.split('/')[-2]
ratings = requests.get(f'https://magicpin.in/sam-api/merchants/get_merchant_reviews/?merchantUserId={mid}').json()['data']
Example
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = "https://magicpin.in/Delhi/search/?dist=10&query=biriyani&rt=3" # URL of the website
header = {'User-Agent':'Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36'} # Temporary user agent
r = requests.get(url, headers=header)
soup = BeautifulSoup(r.content, 'html.parser')
rest_list=[]
divs = soup.find_all('div', class_ ="merchant-card-single")
for item in divs:
title = item.find('h3').text.strip() # restaurant name
loc = item.find('h4', class_ ="merchant-location").text.strip() # restaurant location
try: # used this try and except method because some restaurants are unrated and while scrpaping those we would run into an error
rating = item.find('span', class_="rating-value").text
except:
rating = None
price = item.find('div', class_="cft-timing").text.strip() # price for biriyani
url = item.find('a').get('href')
mid = url.split('/')[-2]
ratings = requests.get(f'https://magicpin.in/sam-api/merchants/get_merchant_reviews/?merchantUserId={mid}').json()['data']
biry_del = {
'name': title,
'location': loc,
'rating': rating,
'ratings': ratings,
'price': price
}
rest_list.append(biry_del)
print(rest_list)
Output
[{'name': 'BBC, Best Biriyani & Chicken', 'location': 'Satyaniketan\n , New Delhi', 'rating': '4.4', 'ratings': [{'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2021-12-02T23:17:47+05:30', 'post_id': 945109867, 'review': '', 'user': {'name': 'Gulzar', 'vanity': '2 Followers', 'badge': None, 'total_visits': 13, 'total_spent': 17641, 'user_id': '10663391', 'description': '', 'deeplink': 'magicpin://profileuser?userId=10663391', 'profile_pic': 'https://lh3.googleusercontent.com/V-R22m0t7GAJGn-bOhzjWXywVWk-GgwT75aBGh5Bq3ZXku8npkIGbQaBCKgnkCwuDFXRlrmbOBhEOTNu3-tXRwjdJOqfw_D_7OSzqDk8=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2021-08-03T14:49:12+05:30', 'post_id': 940277857, 'review': '', 'user': {'name': 'Dattatray Aadhav', 'vanity': '31 Followers', 'badge': None, 'total_visits': 318, 'total_spent': 636061, 'user_id': '6194299', 'description': '', 'deeplink': 'magicpin://profileuser?userId=6194299', 'profile_pic': 'https://lh3.googleusercontent.com/5eA-UBjZ6PBwDD2Pgb15xREya5D9Mma4PEU2Ka4G9MDMR1qwNKGrJO5Z6FMT63SplMo_o8V7xWYTg_oMdh3PeiC1bXUr=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2021-08-03T14:46:52+05:30', 'post_id': 940277730, 'review': "Didn't like the promotions & discounts at this place", 'user': {'name': 'Anshika', 'vanity': '9 Followers', 'badge': None, 'total_visits': 56, 'total_spent': 28936, 'user_id': '9555088', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9555088', 'profile_pic': 'https://lh3.googleusercontent.com/YXgpyigxHNH0OMNnOsYerRxfmHAxxDYd9nsxzSveKwEAV8wrJUYmwabrgu0Ah_GMd7Mup26WjbWtPu-kkTcby6ddCoPQZ1aOCioi1wY6=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 2, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2021-08-03T14:48:56+05:30', 'post_id': 940277460, 'review': 'Loved the quality of products, safety precautions, variety of options, and location of store at this place', 'user': {'name': 'Priyank Jain', 'vanity': '118 Followers', 'badge': None, 'total_visits': 389, 'total_spent': 143081, 'user_id': '1140253', 'description': '', 'deeplink': 'magicpin://profileuser?userId=1140253', 'profile_pic': 'https://lh3.googleusercontent.com/nL4eRFh8Hi-b59CP4cL2cO57olZLxnL7yTiZ6xpoNLG67FQsGtEm-eTrKE9EaZ_cRtcR2TbldWxtqU2oG8Ob0x7Aag=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2021-08-03T14:46:56+05:30', 'post_id': 940277155, 'review': 'Loved the quality of products, safety precautions, variety of options, location of store, and promotions & discounts at this place', 'user': {'name': 'Naman', 'vanity': '5 Followers', 'badge': None, 'total_visits': 105, 'total_spent': 65821, 'user_id': '6148749', 'description': '19MaleChill', 'deeplink': 'magicpin://profileuser?userId=6148749', 'profile_pic': 'https://lh3.googleusercontent.com/C5UMTYVWkz1mNFUKWDfYKtCKDHoSUIrHsifJ7kfWrmJUBxjjyLW1lS0gOs4H31dekHdAXESRwN8cnqKVvxbY4P8GpRihVnZflujJfAjS=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2021-01-28T11:56:12+05:30', 'post_id': 932109603, 'review': 'Loved the range of products, delivery time, and promotions & discounts at this place', 'user': {'name': 'sunil', 'vanity': '', 'badge': None, 'total_visits': 2, 'total_spent': 586, 'user_id': '10156497', 'description': '', 'deeplink': 'magicpin://profileuser?userId=10156497', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-18T16:31:55+05:30', 'post_id': 929075104, 'review': '', 'user': {'name': 'Vrj', 'vanity': '', 'badge': None, 'total_visits': 11, 'total_spent': 34353, 'user_id': '7455242', 'description': '', 'deeplink': 'magicpin://profileuser?userId=7455242', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 4, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-18T16:32:42+05:30', 'post_id': 929074944, 'review': '', 'user': {'name': 'Vaibhav Deshatavar', 'vanity': '2 Followers', 'badge': None, 'total_visits': 26, 'total_spent': 7571, 'user_id': '4271349', 'description': '', 'deeplink': 'magicpin://profileuser?userId=4271349', 'profile_pic': 'https://lh3.googleusercontent.com/LbU80bZNxLLQ_bhc2VqWc6CJXHyqcNCLpBg5YBKMFWogWepbIyen3rdQIZx6WRZjT5l1OR63OLpZOuSVnz2TGXpP4pY=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': 'Niceeee ', 'review_date': '2020-12-18T12:38:07+05:30', 'post_id': 929056653, 'review': 'Loved the range of products, quality of products, delivery time, safe packaging, and promotions & discounts at this place', 'user': {'name': 'JAGGU RAJ', 'vanity': '', 'badge': None, 'total_visits': 3, 'total_spent': 3877, 'user_id': '9567167', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9567167', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-18T09:44:06+05:30', 'post_id': 929047869, 'review': '', 'user': {'name': 'Ramya', 'vanity': '', 'badge': None, 'total_visits': 40, 'total_spent': 307540, 'user_id': '7769558', 'description': '', 'deeplink': 'magicpin://profileuser?userId=7769558', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-18T09:43:13+05:30', 'post_id': 929047672, 'review': '', 'user': {'name': 'Hemraj', 'vanity': '', 'badge': None, 'total_visits': 9, 'total_spent': 7636, 'user_id': '9840082', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9840082', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-18T09:41:35+05:30', 'post_id': 929046779, 'review': 'Loved the quality of service, ambience, safety precautions, and taste at this place', 'user': {'name': 'Rahul', 'vanity': '1 Followers', 'badge': None, 'total_visits': 17, 'total_spent': 6594, 'user_id': '8723539', 'description': '', 'deeplink': 'magicpin://profileuser?userId=8723539', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-17T20:44:06+05:30', 'post_id': 929030684, 'review': 'Loved the quality of products and delivery time at this place', 'user': {'name': 'Mohit', 'vanity': '', 'badge': None, 'total_visits': 4, 'total_spent': 1086, 'user_id': '9878699', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9878699', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:12:24+05:30', 'post_id': 928845159, 'review': 'Loved the quality of products, safety precautions, variety of options, location of store, and promotions & discounts at this place', 'user': {'name': 'Vishal Sorap', 'vanity': '11 Followers', 'badge': None, 'total_visits': 39, 'total_spent': 44997, 'user_id': '2379680', 'description': '', 'deeplink': 'magicpin://profileuser?userId=2379680', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:06:28+05:30', 'post_id': 928844059, 'review': '', 'user': {'name': 'Harshal Rane', 'vanity': '58 Followers', 'badge': None, 'total_visits': 286, 'total_spent': 1263268, 'user_id': '1615250', 'description': '', 'deeplink': 'magicpin://profileuser?userId=1615250', 'profile_pic': 'https://lh3.googleusercontent.com/9eADr5gEJeWHS17inAcfrbke3k-aYBzm-3f6JoP2Kzkljf2sP6-fHE5hxbMw7EI7Hk3q8eZa-gGE1Zes_bXLNDcrpuc=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 3, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': 'Everything \n', 'review_date': '2020-12-14T22:12:22+05:30', 'post_id': 928843503, 'review': 'Loved the quality, service, safety precautions, and promotions at this place', 'user': {'name': 'Aman Bhagat', 'vanity': '28 Followers', 'badge': None, 'total_visits': 48, 'total_spent': 33812, 'user_id': '6589071', 'description': '', 'deeplink': 'magicpin://profileuser?userId=6589071', 'profile_pic': 'https://lh3.googleusercontent.com/jtEV_8v2CjIcrJTPLhL_me_flmHxOwrCesP6Z7J24Y-jvYWWfVvOjUcCCUZ2cvVD55wtUGtn9YupoqSuFyttXGaC6aw=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:08:00+05:30', 'post_id': 928842848, 'review': 'Loved the range of products, quality of products, and delivery time at this place', 'user': {'name': 'Ashok Kumar Baghel', 'vanity': '', 'badge': None, 'total_visits': 79, 'total_spent': 78554, 'user_id': '9374043', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9374043', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:05:44+05:30', 'post_id': 928841967, 'review': '', 'user': {'name': 'Akash dutt', 'vanity': '5 Followers', 'badge': None, 'total_visits': 196, 'total_spent': 711777, 'user_id': '7182223', 'description': '', 'deeplink': 'magicpin://profileuser?userId=7182223', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:07:11+05:30', 'post_id': 928841156, 'review': 'Loved the quality of service, ambience, safety precautions, taste, and price, promotions, & discounts at this place', 'user': {'name': 'Raunak', 'vanity': '44 Followers', 'badge': None, 'total_visits': 250, 'total_spent': 81010, 'user_id': '9210632', 'description': 'I am no one interesting', 'deeplink': 'magicpin://profileuser?userId=9210632', 'profile_pic': 'https://lh3.googleusercontent.com/-RVyHuI5_SABCC0LtLtV2oSCYmTyAJWJ-bv9ZX0lQY7tv6cdJIK0sW7BYTni2Pd6lv7Bb-Te1COJcQa9-2uWf3nkTcY=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T21:57:57+05:30', 'post_id': 928840295, 'review': 'Loved the variety of options, safety precautions, helpful staff, and price, promotions, & discounts at this place', 'user': {'name': 'Prashanth', 'vanity': '8 Followers', 'badge': None, 'total_visits': 75, 'total_spent': 374044, 'user_id': '9821253', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9821253', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:02:35+05:30', 'post_id': 928840224, 'review': 'Loved the range of products at this place', 'user': {'name': 'Sushant P', 'vanity': '39 Followers', 'badge': None, 'total_visits': 407, 'total_spent': 358857, 'user_id': '4789975', 'description': '', 'deeplink': 'magicpin://profileuser?userId=4789975', 'profile_pic': 'https://lh3.googleusercontent.com/_AWRlRrTsNUdZDKyB4mTeFK99T2gM29ScDGVt8j1C5NemAprx_gw3OofqS3_cm4cELfpWWzIie55YYHYHTpLEQ2rpqs=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 4, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': 'Amazon is best ', 'review_date': '2020-12-14T21:55:10+05:30', 'post_id': 928839174, 'review': 'Loved the delivery time and promotions & discounts at this place', 'user': {'name': 'Sunny', 'vanity': '6 Followers', 'badge': None, 'total_visits': 24, 'total_spent': 40934, 'user_id': '9768929', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9768929', 'profile_pic': 'https://lh3.googleusercontent.com/dDT5_ubc0MRR6y541WX6kGbqBBcm7xqeFxBPRUZgTO-x8hLF7fEzEkKzgwhq1r8wPDck-xtZCj99PyrO_i8xQgCA_-KyVV00kS31wBfS=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:01:07+05:30', 'post_id': 928838737, 'review': 'Loved the quality of service, ambience, and taste at this place', 'user': {'name': 'Sohil Merchant', 'vanity': '5 Followers', 'badge': None, 'total_visits': 199, 'total_spent': 98483, 'user_id': '9222147', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9222147', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T21:52:45+05:30', 'post_id': 928838145, 'review': '', 'user': {'name': 'pinki', 'vanity': '1 Followers', 'badge': None, 'total_visits': 7, 'total_spent': 3207, 'user_id': '9836153', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9836153', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:10:12+05:30', 'post_id': 928838005, 'review': '', 'user': {'name': 'Sudhanshu Shekhar', 'vanity': '3 Followers', 'badge': None, 'total_visits': 78, 'total_spent': 522579, 'user_id': '8217943', 'description': '', 'deeplink': 'magicpin://profileuser?userId=8217943', 'profile_pic': 'https://lh3.googleusercontent.com/PduS8X6YNq_gCRe_1sQ0zi6LFDalhGoeDeV_M7BJg_szWV7TLeqm2QotxNMczJDjLEQzJjTq7H3tAEtHLiDobBBguLBc=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 2, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:01:58+05:30', 'post_id': 928837918, 'review': 'Loved the ambience and taste at this place', 'user': {'name': 'Sandip Raul', 'vanity': '43 Followers', 'badge': None, 'total_visits': 87, 'total_spent': 257686, 'user_id': '3179212', 'description': '', 'deeplink': 'magicpin://profileuser?userId=3179212', 'profile_pic': 'https://lh3.googleusercontent.com/3nl1P7BGqdGiVdEGzjJ24SnV-aPDSe8KUv38d_kvo6G82NefqsVvlMN4l9AbDPhldMlWDN7JUgjzQvp9gKNFr9Sz1jt7K4nskEOIkXU=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T21:58:06+05:30', 'post_id': 928837904, 'review': 'Loved the quality of products, safety precautions, variety of options, location of store, and promotions & discounts at this place', 'user': {'name': 'Killer', 'vanity': '7 Followers', 'badge': None, 'total_visits': 28, 'total_spent': 21599, 'user_id': '7412507', 'description': '', 'deeplink': 'magicpin://profileuser?userId=7412507', 'profile_pic': 'https://lh3.googleusercontent.com/_fhBywD7kvez24utn17OhvtbYK-jMvnL9frdFLPxV_vJk2lILIkLE6FRxW58mfTZRBHNB0im-nSGE6GSzLJ3ULJMsgzC=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:05:34+05:30', 'post_id': 928837839, 'review': 'Loved the range of products, quality of products, delivery time, safe packaging, and promotions & discounts at this place', 'user': {'name': 'Axat', 'vanity': '', 'badge': None, 'total_visits': 26, 'total_spent': 22707, 'user_id': '9571962', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9571962', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T21:52:34+05:30', 'post_id': 928837057, 'review': '', 'user': {'name': 'Manna11', 'vanity': '', 'badge': None, 'total_visits': 3, 'total_spent': 1209, 'user_id': '9840078', 'description': '', 'deeplink': 'magicpin://profileuser?userId=9840078', 'profile_pic': '', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 4, 'reply_date': '0001-01-01T00:00:00Z'}, {'rating_text': 'Outlet Rating', 'reply': '', 'merchant': None, 'review_title': '', 'review_date': '2020-12-14T22:10:41+05:30', 'post_id': 928836452, 'review': 'Loved the range of products at this place', 'user': {'name': 'Rushikesh Ingale', 'vanity': '23 Followers', 'badge': None, 'total_visits': 116, 'total_spent': 217734, 'user_id': '6911448', 'description': '', 'deeplink': 'magicpin://profileuser?userId=6911448', 'profile_pic': 'https://lh3.googleusercontent.com/-fb2w-cH1_0rjjoK2GYQDYr1tu_SAF_NyLN7eUHvTATCwO9fP6QEaV_wd6blNamjhJ5radva3axs03SDO46HVhYosPI=s120', 'is_following': False}, 'media_list': [{'image_url': '', 'aspect_ratio': 1}], 'rating': 5, 'reply_date': '0001-01-01T00:00:00Z'}], 'price': 'Average Spent: \n₹350'}]

Json to Pandas, include "Parents"

With a list of 150+ Neighborhoods , I am using Foursquare API to retrieve nearby venues at 500m radius of a given Neighbourhood. Each Neighbourhood is expected to return 10-20 nearby venues.
Refer to snippet of json result as returned by Foursquare.
With results['response']['groups'][0]['items'], I able to retrieve the nearby venues information and make it a Table as below. However results['response']['groups'][0]['items'] does not have the Neighbourhood ( under headerFullLocation in json) of associated venues.
Q: How can I link the Neighbourhood(headerFullLocation) to its associated nearby venue and add it as a column to table below? Thanks for the advice.
{'suggestedFilters': {'header': 'Tap to show:',
'filters': [{'name': 'Open now', 'key': 'openNow'}]},
'headerLocation': 'Alexandra Park',
'headerFullLocation': 'Alexandra Park, Toronto',**
'headerLocationGranularity': 'neighborhood',
'totalResults': 138,
'suggestedBounds': {'ne': {'lat': 43.6545000045, 'lng': -79.39379244047241},
'sw': {'lat': 43.645499995499996, 'lng': -79.4062075595276}},
'groups': [{'type': 'Recommended Places',
'name': 'recommended',
'items': [{'reasons': {'count': 0,
'items': [{'summary': 'This spot is popular',
'type': 'general',
'reasonName': 'globalInteractionReason'}]},
'venue': {'id': '5644dbaa498e7f7534154326',
'**name': 'Maker Pizza',**
'contact': {},
'location': {'address': '59 Cameron St',
'lat': 43.6504011331197,
'lng': -79.39804047841302,
'labeledLatLngs': [{'label': 'display',
'lat': 43.6504011331197,
'lng': -79.39804047841302}],
'distance': 164,
'postalCode': 'M5T 2H1',
'cc': 'CA',
'city': 'Toronto',
'state': 'ON',
'country': 'Canada',
'formattedAddress': ['59 Cameron St', 'Toronto ON M5T 2H1', 'Canada']},
'categories': [{'id': '4bf58dd8d48988d1ca941735',
'name': 'Pizza Place',
'pluralName': 'Pizza Places',
'shortName': 'Pizza',
'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/pizza_',
'suffix': '.png'},
'primary': True}],
'verified': False,
'stats': {'tipCount': 0,
'usersCount': 0,
'checkinsCount': 0,
'visitsCount': 0},
'beenHere': {'count': 0,
'lastCheckinExpiredAt': 0,
'marked': False,
'unconfirmedCount': 0},
'photos': {'count': 0, 'groups': []},
'hereNow': {'count': 0, 'summary': 'Nobody here', 'groups': []}},
Why don't you just do venues['Neighbourhood'] = response['headerFullLocation']. I am assuming, you send a separate request for each neigbhourhood and plan to concatenate multiple venue dataframes in the end.

Categories

Resources