Created
August 13, 2019 08:51
-
-
Save Beomi/ee08524b66cd4f8a06d0c7c67f97b920 to your computer and use it in GitHub Desktop.
Twitter Follow counter by Username
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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