Skip to content

Instantly share code, notes, and snippets.

@Beomi
Created August 13, 2019 08:51
Show Gist options
  • Save Beomi/ee08524b66cd4f8a06d0c7c67f97b920 to your computer and use it in GitHub Desktop.
Save Beomi/ee08524b66cd4f8a06d0c7c67f97b920 to your computer and use it in GitHub Desktop.
Twitter Follow counter by Username
import requests
from bs4 import BeautifulSoup as bs
def _word_to_int(x):
if x.isdigit():
return int(x)
if 'K' in x:
return int(float(x[:-1]) * 1000)
def get_follow_counts(username):
r = requests.get(f'https://twitter.com/{username}')
soup = bs(r.text, 'html.parser')
followers = soup.select_one(f'a[href="/{username}/followers"]').text.split()[-1]
followings = soup.select_one(f'a[href="/{username}/following"]').text.split()[-1]
return _word_to_int(followings), _word_to_int(followers)
# Usage
get_follow_counts('faker')
# (19, 140000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment