Last active
September 17, 2020 15:47
-
-
Save ELC/6a0f1716bc39d0ef21285722f0c57bd3 to your computer and use it in GitHub Desktop.
EMSS2020
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
chromium-chromedriver |
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from pprint import pprint\n", | |
"import time\n", | |
"from collections import defaultdict\n", | |
"from functools import partial\n", | |
"from time import perf_counter\n", | |
"\n", | |
"from tqdm.notebook import tqdm\n", | |
"from selenium.common.exceptions import TimeoutException\n", | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"from helium import *" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Helium" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def scroll_helium(driver):\n", | |
" def get_height():\n", | |
" return driver.find_elements_by_xpath('/html/body/ytd-app')[0].size['height']\n", | |
" \n", | |
" last_height = get_height()\n", | |
"\n", | |
" while True:\n", | |
" press(END)\n", | |
" \n", | |
" try:\n", | |
" wait_until(lambda : get_height() > last_height, timeout_secs=5)\n", | |
" except TimeoutException:\n", | |
" break\n", | |
" \n", | |
" last_height = get_height()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def find_text(text_list, text):\n", | |
" for text_elem in text_list:\n", | |
" aria = text_elem.get_attribute('aria-label')\n", | |
" if aria is None:\n", | |
" continue\n", | |
" \n", | |
" if text.lower() in aria.lower():\n", | |
" return aria\n", | |
" \n", | |
" return Exception()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Headless Helium Scrapping" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def scroll_until_found(func):\n", | |
" press(PAGE_DOWN)\n", | |
" return func() " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def headless_download_stats(channel, link=None):\n", | |
" \n", | |
" path_safe = \"0123456789abcdefghijklmnopqrstuvwxyz\"\n", | |
" \n", | |
" start = perf_counter()\n", | |
" \n", | |
" driver = start_chrome(headless=True)\n", | |
" \n", | |
" if link is None:\n", | |
" link = \"https://www.youtube.com/channel/\"\n", | |
" url = f'{link}{channel}/'\n", | |
" else:\n", | |
" url = link\n", | |
" \n", | |
" go_to(url)\n", | |
" \n", | |
" click(Text(\"VIDEOS\"))\n", | |
" \n", | |
" name_raw = driver.find_elements_by_class_name('ytd-channel-name')[0].text\n", | |
" name = ''.join(char for char in name_raw if char.lower() in path_safe)\n", | |
" print(f\"Scrapping for {name} started\")\n", | |
" \n", | |
" wait_until(lambda : len(driver.find_elements_by_xpath('//*[@id=\"icon-label\"]')) != 0)\n", | |
" scroll_helium(driver)\n", | |
"\n", | |
" videos = driver.find_elements_by_xpath('//*[@id=\"video-title\"]')\n", | |
" video_data = defaultdict(list)\n", | |
" \n", | |
" video_data['title'] = [video.text for video in videos]\n", | |
" video_data['link'] = [video.get_attribute('href') for video in videos]\n", | |
" video_data['channel'] = [name] * len(videos)\n", | |
"\n", | |
" for video_link in tqdm(video_data['link']):\n", | |
" go_to(video_link)\n", | |
" wait_until(lambda : len(driver.find_elements_by_xpath('//*[@id=\"date\"]')) != 0)\n", | |
"\n", | |
" try:\n", | |
" views_raw = driver.find_element_by_class_name('view-count').text\n", | |
" views = int(views_raw.split()[0].replace(\",\", \"\"))\n", | |
" except:\n", | |
" views = np.nan\n", | |
"\n", | |
" video_data[\"views_count\"].append(views)\n", | |
" \n", | |
" \n", | |
" try:\n", | |
" video_date = driver.find_elements_by_xpath('//*[@id=\"date\"]')[0].text[1:]\n", | |
" except:\n", | |
" video_date = \"\"\n", | |
" \n", | |
" video_data[\"date\"].append(video_date)\n", | |
" \n", | |
"\n", | |
" wait_until(lambda : len(driver.find_elements_by_xpath('//*[@id=\"text\"]')) >= 7)\n", | |
"\n", | |
" text_in_page = driver.find_elements_by_xpath('//*[@id=\"text\"]')\n", | |
"\n", | |
" try:\n", | |
" likes_raw = find_text(text_in_page, 'like')\n", | |
" likes = int(likes_raw.split()[0].replace(\",\", \"\"))\n", | |
" except:\n", | |
" likes = np.nan\n", | |
" \n", | |
" video_data[\"likes\"].append(likes)\n", | |
"\n", | |
" try:\n", | |
" dislikes_raw = find_text(text_in_page, 'dislike')\n", | |
" dislikes = int(dislikes_raw.split()[0].replace(\",\", \"\"))\n", | |
" except:\n", | |
" dislikes = np.nan\n", | |
" \n", | |
" video_data[\"dislikes\"].append(dislikes)\n", | |
"\n", | |
" try:\n", | |
" wait_until(lambda : scroll_until_found(lambda : len(driver.find_elements_by_class_name('count-text')) >= 1),\n", | |
" timeout_secs=40, interval_secs=1)\n", | |
" comments_count_raw = driver.find_elements_by_class_name('count-text')[0].text\n", | |
" comments_count = int(comments_count_raw.split()[0].replace(\",\", \"\"))\n", | |
" except:\n", | |
" comments_count = np.nan\n", | |
" \n", | |
" video_data[\"comments_count\"].append(comments_count)\n", | |
"\n", | |
" kill_browser()\n", | |
" \n", | |
" elapsed = perf_counter() - start\n", | |
" print(f\"Time Elapsed: {elapsed:2.2f}s\")\n", | |
" \n", | |
" df = pd.DataFrame(video_data)\n", | |
" \n", | |
" df.to_csv(f'{name}_data.csv', index=False)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Scrapping for PBSInfiniteSeries started\n" | |
] | |
}, | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "5c8abe45ce294737ba8ebc3141ea3bf8", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"HBox(children=(FloatProgress(value=0.0, max=65.0), HTML(value='')))" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"1028.67\n" | |
] | |
} | |
], | |
"source": [ | |
"channel = \"UCs4aHmggTfFrpkPcWSaBN9g\" # PBS Infinite Series Channel ID\n", | |
"try:\n", | |
" headless_download_stats(channel)\n", | |
"except:\n", | |
" kill_browser()" | |
] | |
} | |
], | |
"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.7.7" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
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
name: EMSS-Castano | |
channels: | |
- conda-forge | |
dependencies: | |
- python=3.7 | |
- numpy | |
- pandas | |
- scipy | |
- seaborn | |
- matplotlib | |
- selenium | |
- tqdm | |
- pymc3 | |
- mkl-service | |
- pip: | |
- helium |
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
views_count | date | likes | dislikes | comments_count | |
---|---|---|---|---|---|
118579 | 17 May 2018 | 4243 | 675 | 1290 | |
102081 | 17 May 2018 | 1731 | 143 | 375 | |
54220 | 26 Apr 2018 | 1415 | 65 | 188 | |
60052 | 13 Apr 2018 | 1490 | 37 | 141 | |
37976 | 29 Mar 2018 | 1569 | 42 | 167 | |
98477 | 23 Mar 2018 | 2471 | 69 | 784 | |
43906 | 15 Mar 2018 | 1080 | 51 | 208 | |
82826 | 8 Mar 2018 | 2412 | 105 | 398 | |
77348 | 1 Mar 2018 | 2257 | 26 | 377 | |
82363 | 27 Feb 2018 | 3229 | 49 | 608 | |
77653 | 15 Feb 2018 | 2008 | 46 | 278 | |
133253 | 1 Feb 2018 | 3354 | 186 | 376 | |
140062 | 25 Jan 2018 | 4496 | 90 | 639 | |
69011 | 18 Jan 2018 | 1869 | 344 | 315 | |
60526 | 11 Jan 2018 | 2126 | 34 | 187 | |
67910 | 21 Dec 2017 | 1974 | 267 | 359 | |
63290 | 14 Dec 2017 | 2872 | 34 | 249 | |
84684 | 12 Dec 2017 | 2798 | 49 | 330 | |
70405 | 30 Nov 2017 | 2721 | 120 | 404 | |
104901 | 23 Nov 2017 | 4251 | 201 | 613 | |
139189 | 17 Nov 2017 | 6433 | 95 | 954 | |
114225 | 10 Nov 2017 | 3171 | 86 | 262 | |
703089 | 19 Oct 2017 | 17149 | 991 | 2374 | |
144368 | 12 Oct 2017 | 4645 | 75 | 547 | |
69256 | 6 Oct 2017 | 1845 | 23 | 207 | |
101029 | 28 Sep 2017 | 2907 | 58 | 319 | |
127403 | 21 Sep 2017 | 3748 | 80 | 540 | |
139737 | 14 Sep 2017 | 3635 | 81 | 684 | |
226200 | 24 Aug 2017 | 6287 | 130 | 569 | |
102046 | 23 Aug 2017 | 2929 | 44 | 275 | |
137518 | 22 Aug 2017 | 3799 | 43 | 259 | |
88791 | 10 Aug 2017 | 2985 | 190 | 659 | |
265561 | 3 Aug 2017 | 8735 | 197 | 768 | |
346312 | 21 Jul 2017 | 9094 | 644 | 1036 | |
114411 | 13 Jul 2017 | 2824 | 63 | 392 | |
83106 | 29 Jun 2017 | 2149 | 45 | 351 | |
122523 | 22 Jun 2017 | 2359 | 109 | 607 | |
106887 | 15 Jun 2017 | 3910 | 45 | 601 | |
92765 | 8 Jun 2017 | 3078 | 90 | 420 | |
98660 | 1 Jun 2017 | 4007 | 46 | 402 | |
222122 | 19 May 2017 | 5939 | 188 | 889 | |
126756 | 11 May 2017 | 3710 | 60 | 487 | |
103940 | 4 May 2017 | 2810 | 68 | 413 | |
198880 | 27 Apr 2017 | 4721 | 83 | 418 | |
210136 | 20 Apr 2017 | 5472 | 104 | 480 | |
193191 | 13 Apr 2017 | 4576 | 158 | 919 | |
23375 | 10 Apr 2017 | 1415 | 62 | 275 | |
89182 | 6 Apr 2017 | 2375 | 53 | 386 | |
174447 | 23 Mar 2017 | 4461 | 98 | 488 | |
91416 | 16 Mar 2017 | 2561 | 53 | 377 | |
168412 | 9 Mar 2017 | 5278 | 110 | 662 | |
312267 | 2 Mar 2017 | 6188 | 419 | 1156 | |
72497 | 23 Feb 2017 | 2103 | 57 | 284 | |
566168 | 16 Feb 2017 | 14809 | 336 | 1377 | |
158059 | 2 Feb 2017 | 3683 | 53 | 546 | |
241847 | 26 Jan 2017 | 6259 | 185 | 997 | |
313571 | 19 Jan 2017 | 7921 | 140 | 980 | |
153137 | 12 Jan 2017 | 4617 | 59 | 606 | |
274378 | 5 Jan 2017 | 8077 | 404 | 1304 | |
143995 | 22 Dec 2016 | 3565 | 75 | 403 | |
169567 | 15 Dec 2016 | 4033 | 70 | 968 | |
212652 | 8 Dec 2016 | 5400 | 145 | 1134 | |
111195 | 1 Dec 2016 | 3529 | 46 | 548 | |
318748 | 24 Nov 2016 | 8585 | 352 | 1826 | |
872483 | 17 Nov 2016 | 23675 | 745 | 2391 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment