Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary within html code
#1
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
Reply
#2
what does the HTML code look like?
please supply
Reply
#3
(Aug-09-2021, 10:15 AM)Larz60+ Wrote: what does the HTML code look like?
please supply
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
Reply
#4
Does Facebook not have an API endpoint you can use to get the page view data?
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 923 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  reading html and edit chekcbox to html jacklee26 5 3,071 Jul-01-2021, 10:31 AM
Last Post: snippsat
  Iterating over a dictionary in a for loop - checking code has worked sallyjc81 1 1,922 Dec-29-2020, 05:14 PM
Last Post: ndc85430
  Line of code to show dictionary doesn't work MaartenRo 2 2,425 Jul-28-2020, 03:58 PM
Last Post: deanhystad
  How does this code know to use the key of a dictionary renveg 1 1,444 Feb-15-2020, 11:10 PM
Last Post: scidam
  HTML to Python to Windows .bat and back to HTML perfectservice33 0 1,944 Aug-22-2019, 06:31 AM
Last Post: perfectservice33
  Save code with output to HTML file type agohir 2 2,858 Mar-26-2019, 05:17 PM
Last Post: agohir
  Why does my code not execute? (def function with 'for in' iteration over a dictionary Placebo 3 2,923 Oct-19-2018, 01:32 PM
Last Post: Placebo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020