Get username field in Facebook Graph API 2.0

后端 未结 8 518
忘了有多久
忘了有多久 2020-11-29 02:55

The \"old\" Facebook Graph API had a \"username\" field which could be used to create a human-readable profile URL. My username for example is \"sebastian.trug\" which resul

8条回答
  •  再見小時候
    2020-11-29 03:16

    Inspired from @RifkiFauzi 's answer, here is my solution in pure Python

    #get html of a page via pure python ref. https://stackoverflow.com/a/23565355/248616
    import requests
    r = requests.get('http://fb.com/%s' % FB_USER_ID) #open profile page of the facebook user
    r.raise_for_status()
    html = r.content
    
    #search string with regex ref. https://stackoverflow.com/a/4667014/248616
    import re
    # m = re.search('meta http-equiv="refresh" content="0; URL=/([^?]+)\?', html)
    m = re.search('a class="profileLink" href="([^"]+)"', html)
    href = m.group(1) #will be https://www.facebook.com/$FB_USER_NAME on 201705.24
    username = href.split('/')[-1]
    print(href)
    print(username)
    

提交回复
热议问题