![]() |
Dictionary within html code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Dictionary within html code (/thread-34557.html) |
Dictionary within html code - ebolisa - Aug-09-2021 Hi, I'd like to list all the visitors who have viewed my Facebook page. For that I use CTR-U to get to my page's HTML code and CTR-F to search for the text "buddy_id" or "name". Both are listed within a <script></script> code and in a dictionary format. How to strip the html code and get directly to the dictionary content? TIA import json filename = 'facebook.txt' # html code try: with open(filename) as f: data = f.read() print("Data type before reconstruction : ", type(data)) # reconstructing the data as a dictionary js = json.loads(data) print("Data type after reconstruction : ", type(js)) print(js) except Exception as e: print('Error msg: ', e) exit() # or return #if an error, exit RE: Dictionary within html code - Larz60+ - Aug-09-2021 what does the HTML code look like? please supply RE: Dictionary within html code - ebolisa - Aug-09-2021 (Aug-09-2021, 10:15 AM)Larz60+ Wrote: what does the HTML code look like?Hi, for privacy reasons, I cannot do that but it looks like a standard html code: <!DOCTYPE html><html id="facebook" ....> <head></head> <body> <script>requireLazy(...)</script> <--- dictionary is here </body></html>This code got me closer once I stripped the html code manually: import json filename = 'facebook.txt' # html code try: with open(filename) as f: data = f.read() print("Data type before reconstruction : ", type(data)) # reconstructing the data as a dictionary js = json.loads(data) print("Data type after reconstruction : ", type(js)) # print(js) for x in js[1]['__bbox']['result']['data']['viewer']['chat_sidebar_contact_rankings'][0]['user']: print(js[1]['__bbox']['result']['data']['viewer']['chat_sidebar_contact_rankings'][0]['user']['name']) except Exception as e: print('Error msg: ', e) exit() # or return #if an error, exit RE: Dictionary within html code - ndc85430 - Aug-09-2021 Does Facebook not have an API endpoint you can use to get the page view data? RE: Dictionary within html code - ebolisa - Aug-09-2021 (Aug-09-2021, 11:34 AM)ndc85430 Wrote: Does Facebook not have an API endpoint you can use to get the page view data?I'm not familiar with it but will search. Thanks. |