Issue
I want to get "Ilan No" from the following JSON object.
With Javascript I can get it with these commands:
var jsons= document.getElementById("gaPageViewTrackingJson")
var housePrice = $(jsons).data('json')['customVars'][11].value
With Python I can't figure out how to get...
<div data-json='{"trackPageview":["ilan_emlak_konut_satilik_turkiye_ankara_kecioren","view_ilan_detay"],"customVars":[{"index":1,"name":"cat1","value":"Emlak","scope":2},{"index":1,"name":"cat2","value":"Konut","scope":2},{"index":1,"name":"cat3","value":"Satılık","scope":2},{"index":1,"name":"cat4","value":"Daire","scope":2},{"index":1,"name":"cat5","value":"","scope":2},{"index":1,"name":"loc1","value":"Türkiye","scope":2},{"index":1,"name":"loc2","value":"Ankara","scope":2},{"index":1,"name":"loc3","value":"Keçiören","scope":2},{"index":1,"name":"loc4","value":"Bağlum","scope":2},{"index":1,"name":"loc5","value":"Hisar Mah.","scope":2},{"index":1,"name":"ilan_fiyat","value":" 839.000 TL","scope":2},{"index":1,"name":"İlan No","value":"1038908710","scope":2},{"index":1,"name":"İlan Tarihi","value":"15 Temmuz 2022","scope":2},{"index":1,"name":"Emlak Tipi","value":"Satılık Daire","scope":2},{"index":1,"name":"m² (Brüt)","value":"145","scope":2},{"index":1,"name":"m² (Net)","value":"135","scope":2},{"index":1,"name":"Oda Sayısı","value":"3+1","scope":2},{"index":1,"name":"Bina Yaşı","value":"4","scope":2},{"index":1,"name":"Bulunduğu Kat","value":"3","scope":2},{"index":1,"name":"Kat Sayısı","value":"4","scope":2},{"index":1,"name":"Isıtma","value":"Doğalgaz (Kombi)","scope":2},{"index":1,"name":"Banyo Sayısı","value":"2","scope":2},{"index":1,"name":"Balkon","value":"Var","scope":2},{"index":1,"name":"Eşyalı","value":"Hayır","scope":2},{"index":1,"name":"Kullanım Durumu","value":"Boş","scope":2},{"index":1,"name":"Site İçerisinde","value":"Hayır","scope":2},{"index":1,"name":"Site Adı","value":"Belirtilmemiş","scope":2},{"index":1,"name":"Aidat (TL)","value":"50","scope":2},{"index":1,"name":"Krediye Uygun","value":"Evet","scope":2},{"index":1,"name":"Tapu Durumu","value":"Kat Mülkiyetli","scope":2},{"index":1,"name":"Kimden","value":"Emlak Ofisinden","scope":2},{"index":1,"name":"Görüntülü Arama İle Gezilebilir","value":"Hayır","scope":2},{"index":1,"name":"Takas","value":"Evet","scope":2},{"index":1,"name":"Geçici Numara Servisi","value":"Hayır","scope":2},{"index":1,"name":"site_preference","value":"desktop","scope":2},{"index":1,"name":"is_param_guvendeli","value":"0","scope":3},{"index":1,"name":"premium_emlak","value":"0","scope":3},{"index":1,"name":"fotobot","value":"1","scope":3}],"trackEvent":[],"dmpData":[{"name":"satilik_m2_brutfiyat","value":"5000-6000"},{"name":"cat1","value":"emlak"},{"name":"cat2","value":"konut"},{"name":"cat3","value":"satilik"},{"name":"cat4","value":"daire"},{"name":"cat0","value":"emlak-konut-satilik-daire"},{"name":"loc1","value":"turkiye"},{"name":"loc2","value":"ankara"},{"name":"loc3","value":"kecioren"},{"name":"loc4","value":"baglum"},{"name":"loc5","value":"hisar-mah."},{"name":"m2_brut","value":"145"},{"name":"m2_net","value":"135"},{"name":"oda_sayisi","value":"3-1"},{"name":"bina_yasi","value":"4"},{"name":"bulundugu_kat","value":"3"},{"name":"kat_sayisi","value":"4"},{"name":"isitma","value":"dogalgaz-kombi"},{"name":"banyo_sayisi","value":"2"},{"name":"balkon","value":"var"},{"name":"esyali","value":"hayir"},{"name":"kullanim_durumu","value":"bos"},{"name":"site_icerisinde","value":"hayir"},{"name":"site_adi","value":"belirtilmemis"},{"name":"aidat_tl","value":"50"},{"name":"krediye_uygun","value":"evet"},{"name":"tapu_durumu","value":"kat-mulkiyetli"},{"name":"kimden","value":"emlak-ofisinden"},{"name":"goruntulu_arama_ile_gezilebilir","value":"hayir"},{"name":"fiyat","value":"839000"},{"name":"ilan_aks","value":"bakan"}],"transactionObject":null,"transactionUserItem":null,"gaParisClassified":null,"trackCurrentPage":true,"dc":"gcp0","homepageDesign":null,"route":"classified","view":"classified","viewName":"ilan_detay","query":"","categories":[],"locations":[],"classified":{"categories":["Emlak","Konut","Satılık"],"locations":["Türkiye","Ankara","Keçiören"],"isStore":true,"isGet":false,"isActive":true,"isUsed":null},"searchResult":null,"storeSubdomain":null,"successfulPaymentWithSavedCC":false}' id="gaPageViewTrackingJson"></div>
In Python I can't go farther than that
soup = BeautifulSoup(houseData, "html.parser")
data_json = soup.find("div", {"id": "gaPageViewTrackingJson"})
How can $(jsons).data('json')['customVars'][11].value
'"name":"İlan No","value":"1038908710"'
from this object?
Solution
Extract the attribute value and use json.loads()
to convert the string to JSON / dict:
json.loads(soup.find("div", {"id": "gaPageViewTrackingJson"}).get('data-json'))
Example
from bs4 import BeautifulSoup
import json
html='''
<div data-json='{"trackPageview":["ilan_emlak_konut_satilik_turkiye_ankara_kecioren","view_ilan_detay"],"customVars":[{"index":1,"name":"cat1","value":"Emlak","scope":2},{"index":1,"name":"cat2","value":"Konut","scope":2},{"index":1,"name":"cat3","value":"Satılık","scope":2},{"index":1,"name":"cat4","value":"Daire","scope":2},{"index":1,"name":"cat5","value":"","scope":2},{"index":1,"name":"loc1","value":"Türkiye","scope":2},{"index":1,"name":"loc2","value":"Ankara","scope":2},{"index":1,"name":"loc3","value":"Keçiören","scope":2},{"index":1,"name":"loc4","value":"Bağlum","scope":2},{"index":1,"name":"loc5","value":"Hisar Mah.","scope":2},{"index":1,"name":"ilan_fiyat","value":" 839.000 TL","scope":2},{"index":1,"name":"İlan No","value":"1038908710","scope":2},{"index":1,"name":"İlan Tarihi","value":"15 Temmuz 2022","scope":2},{"index":1,"name":"Emlak Tipi","value":"Satılık Daire","scope":2},{"index":1,"name":"m² (Brüt)","value":"145","scope":2},{"index":1,"name":"m² (Net)","value":"135","scope":2},{"index":1,"name":"Oda Sayısı","value":"3+1","scope":2},{"index":1,"name":"Bina Yaşı","value":"4","scope":2},{"index":1,"name":"Bulunduğu Kat","value":"3","scope":2},{"index":1,"name":"Kat Sayısı","value":"4","scope":2},{"index":1,"name":"Isıtma","value":"Doğalgaz (Kombi)","scope":2},{"index":1,"name":"Banyo Sayısı","value":"2","scope":2},{"index":1,"name":"Balkon","value":"Var","scope":2},{"index":1,"name":"Eşyalı","value":"Hayır","scope":2},{"index":1,"name":"Kullanım Durumu","value":"Boş","scope":2},{"index":1,"name":"Site İçerisinde","value":"Hayır","scope":2},{"index":1,"name":"Site Adı","value":"Belirtilmemiş","scope":2},{"index":1,"name":"Aidat (TL)","value":"50","scope":2},{"index":1,"name":"Krediye Uygun","value":"Evet","scope":2},{"index":1,"name":"Tapu Durumu","value":"Kat Mülkiyetli","scope":2},{"index":1,"name":"Kimden","value":"Emlak Ofisinden","scope":2},{"index":1,"name":"Görüntülü Arama İle Gezilebilir","value":"Hayır","scope":2},{"index":1,"name":"Takas","value":"Evet","scope":2},{"index":1,"name":"Geçici Numara Servisi","value":"Hayır","scope":2},{"index":1,"name":"site_preference","value":"desktop","scope":2},{"index":1,"name":"is_param_guvendeli","value":"0","scope":3},{"index":1,"name":"premium_emlak","value":"0","scope":3},{"index":1,"name":"fotobot","value":"1","scope":3}],"trackEvent":[],"dmpData":[{"name":"satilik_m2_brutfiyat","value":"5000-6000"},{"name":"cat1","value":"emlak"},{"name":"cat2","value":"konut"},{"name":"cat3","value":"satilik"},{"name":"cat4","value":"daire"},{"name":"cat0","value":"emlak-konut-satilik-daire"},{"name":"loc1","value":"turkiye"},{"name":"loc2","value":"ankara"},{"name":"loc3","value":"kecioren"},{"name":"loc4","value":"baglum"},{"name":"loc5","value":"hisar-mah."},{"name":"m2_brut","value":"145"},{"name":"m2_net","value":"135"},{"name":"oda_sayisi","value":"3-1"},{"name":"bina_yasi","value":"4"},{"name":"bulundugu_kat","value":"3"},{"name":"kat_sayisi","value":"4"},{"name":"isitma","value":"dogalgaz-kombi"},{"name":"banyo_sayisi","value":"2"},{"name":"balkon","value":"var"},{"name":"esyali","value":"hayir"},{"name":"kullanim_durumu","value":"bos"},{"name":"site_icerisinde","value":"hayir"},{"name":"site_adi","value":"belirtilmemis"},{"name":"aidat_tl","value":"50"},{"name":"krediye_uygun","value":"evet"},{"name":"tapu_durumu","value":"kat-mulkiyetli"},{"name":"kimden","value":"emlak-ofisinden"},{"name":"goruntulu_arama_ile_gezilebilir","value":"hayir"},{"name":"fiyat","value":"839000"},{"name":"ilan_aks","value":"bakan"}],"transactionObject":null,"transactionUserItem":null,"gaParisClassified":null,"trackCurrentPage":true,"dc":"gcp0","homepageDesign":null,"route":"classified","view":"classified","viewName":"ilan_detay","query":"","categories":[],"locations":[],"classified":{"categories":["Emlak","Konut","Satılık"],"locations":["Türkiye","Ankara","Keçiören"],"isStore":true,"isGet":false,"isActive":true,"isUsed":null},"searchResult":null,"storeSubdomain":null,"successfulPaymentWithSavedCC":false}' id="gaPageViewTrackingJson"></div>
'''
soup = BeautifulSoup(html)
jsonData = json.loads(soup.find("div", {"id": "gaPageViewTrackingJson"}).get('data-json'))
## to print the full dict -> {'index': 1, 'name': 'İlan No', 'value': '1038908710', 'scope': 2}
print(jsonData['customVars'][11])
## to print a single value -> 1038908710
print(jsonData['customVars'][11].get('value'))
Answered By - HedgeHog
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.