Webscraping Instagram follower count BeautifulSoup

后端 未结 5 1993
暖寄归人
暖寄归人 2021-01-18 21:12

I\'m just starting to learn how to web scrape using BeautifulSoup and want to write a simple program that will get the follower count for a given Instagram

5条回答
  •  孤城傲影
    2021-01-18 22:15

    Use the API is the easiest way, but I also found a very hacky way to do it:

    import requests
    
    username = "espn"
    url = 'https://www.instagram.com/' + username
    r = requests.get(url).text
    
    start = '"edge_followed_by":{"count":'
    end = '},"followed_by_viewer"'
    followers= r[r.find(start)+len(start):r.rfind(end)]
    
    start = '"edge_follow":{"count":'
    end = '},"follows_viewer"'
    following= r[r.find(start)+len(start):r.rfind(end)]
    
    print(followers, following)
    

    If you look through the response requests gives, theres a line of Javascript that contains the real follower count:

    ...edge_followed_by":{"count":10770969},"followed_by_viewer":{...

    So I just extracted the number by finding the substring before and after.

提交回复
热议问题