Skip to content

Instantly share code, notes, and snippets.

@kaustavbhattacharjee
Created September 25, 2019 07:15
Show Gist options
  • Save kaustavbhattacharjee/43b1bc180cfa6852e76305dd2761d7f6 to your computer and use it in GitHub Desktop.
Save kaustavbhattacharjee/43b1bc180cfa6852e76305dd2761d7f6 to your computer and use it in GitHub Desktop.
People compare the number of subscribers in order to gauge the popularity of an Youtube channel. We propose to use the total number of views on the channel to determine popularity.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# YT_Views_Comparison\n",
"### This project is a part of the assignment for the class IS 665 of Fall 2019.\n",
"##### *People compare the number of subscribers in order to gauge the popularity of an Youtube channel. We propose to use the total number of views on the channel to determine popularity.*\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from bs4 import BeautifulSoup"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"link1 = 'https://www.youtube.com/user/PewDiePie/about'\n",
"link2 = 'https://www.youtube.com/user/tseries/about'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"raw_details1 = requests.get(link1)\n",
"raw_details2 = requests.get(link2)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"soup_user1 = BeautifulSoup(raw_details1.text, \"html.parser\")\n",
"soup_user2 = BeautifulSoup(raw_details2.text, \"html.parser\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"content_user1 = soup_user1.find(\"span\", {\"class\": \"about-stat\"})\n",
"content_user2 = soup_user2.find(\"span\", {\"class\": \"about-stat\"})"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"user1_views=content_user1.get_text().replace(\",\", \"\").replace(\"views\", \"\").replace(\"•\", \"\").strip()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"user2_views=content_user2.get_text().replace(\",\", \"\").replace(\"views\", \"\").replace(\"•\", \"\").strip()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"diff=int(user1_views)-int(user2_views)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"User2 is viewed more by User1 by 60,077,067,145 views\n"
]
}
],
"source": [
"if(diff>0):\n",
" print(\"User1 is viewed more by User2 by \"+\"{:,}\".format(diff)+\" views\")\n",
"elif(diff<0):\n",
" print(\"User2 is viewed more by User1 by \"+\"{:,}\".format(abs(diff))+\" views\")\n",
"else:\n",
" print(\"Both users have equal number of views\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*This project is inspired from [this fanstastic idea!](https://nbviewer.jupyter.org/github/Tanu-N-Prabhu/Python/blob/master/Scraping_two_YouTube_accounts_using_python_libraries.ipynb)*"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment