0

Because I dont have any kind of experience i came to experts :D

So... i want to count the number of videos uploaded on some profiles, but the problem i have is that sometimes i have good results and sometimes its showing me 0, but specific users have like 100+ videos... can someone help me and explain where I made a mistake?

import requests

# ANSI escape sequences for text color
RED = "\033[91m"
GREEN = "\033[92m"
RESET = "\033[0m"

def count_videos(username):
    api_url = f"https://www.tiktok.com/@{username}?is_copy_url=1&is_from_webapp=v1"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36",
    }

    try:
        response = requests.get(api_url, headers=headers)
        if response.status_code == 200:
            html_content = response.text
            count_start = html_content.find('"videoCount":') + len('"videoCount":')
            count_end = html_content.find(',', count_start)
            video_count = int(html_content[count_start:count_end])
            return video_count
        else:
            print(f"Error for: {username}")
            return None
    except requests.exceptions.RequestException as e:
        print(f"A aparut o eroare: {e}")
        return None

def main():
    while True:
        username = input("TikTok Profile: ")
        if username.lower() == "exit":
            break

        video_count = count_videos(username)
        if video_count is not None:
            print(f"Profile '{RED}{username}{RESET}' have {GREEN}{video_count}{RESET} videos.")

    print("Exiting the program.")

if __name__ == "__main__":
    main()

No ideea

  • This happens because sometimes the first instance of the `'"videoCount":'` sometimes legitimately has a `0` as the value. Perhaps [find every instance](https://stackoverflow.com/questions/4664850/how-to-find-all-occurrences-of-a-substring) of `'"videoCount":'` and then pick whichever instance has the highest value. Not a perfect solution but works for larger creators great. – SanguineL May 24 '23 at 14:47

0 Answers0