{"id":4637,"date":"2020-01-02T18:41:38","date_gmt":"2020-01-02T17:41:38","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=4637"},"modified":"2020-01-02T18:49:29","modified_gmt":"2020-01-02T17:49:29","slug":"get-data-from-youtube-videos-with-python-tkinter-gui","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/get-data-from-youtube-videos-with-python-tkinter-gui\/","title":{"rendered":"Get Data From Youtube Videos with Python (tkinter gui)"},"content":{"rendered":"<p>This code lets you <strong>get<\/strong> the <strong>data<\/strong> from a <strong>video<\/strong> on youtune in terms of <strong>views<\/strong> and <strong>subscription<\/strong> (and author) of the video. It is not mine, I just added the code to save the <strong>json<\/strong> file with the date and a progressive number, in case you save more files in a day and a little <strong>graphic<\/strong> interface. It could be useful if you want to <strong>take trace<\/strong> of the views on your videos and do some <strong>statistics<\/strong>, even if there are a lot of statistics that youtube lets you know, but in this case you can personalize them and have access to them without having to go in youtube.<\/p>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/python\r\n# -*- coding: utf-8 -*-\r\n\r\nimport urllib.request\r\nimport urllib.parse\r\nimport urllib.error\r\nfrom bs4 import BeautifulSoup\r\nimport ssl\r\nimport json\r\nimport ast\r\nimport json\r\nimport os\r\nfrom urllib.request import Request, urlopen\r\nfrom datetime import date\r\nimport tkinter as tk\r\n# For ignoring SSL certificate errors\r\n\r\nctx = ssl.create_default_context()\r\nctx.check_hostname = False\r\nctx.verify_mode = ssl.CERT_NONE\r\n\r\n\r\ndef get_data():\r\n    global url\r\n    req = Request(url, headers={'User-Agent': 'Mozilla\/5.0'})\r\n    webpage = urlopen(req).read()\r\n\r\n    # Creating a BeautifulSoup object of the html page for easy extraction of data.\r\n\r\n    soup = BeautifulSoup(webpage, 'html.parser')\r\n    html = soup.prettify('utf-8')\r\n    video_details = {}\r\n    other_details = {}\r\n    for span in soup.findAll('span',attrs={'class': 'watch-title'}):\r\n        video_details['TITLE'] = span.text.strip()\r\n    for script in soup.findAll('script',attrs={'type': 'application\/ld+json'}):\r\n            channelDesctiption = json.loads(script.text.strip())\r\n            video_details['CHANNEL_NAME'] = channelDesctiption['itemListElement'][0]['item']['name']\r\n    for div in soup.findAll('div',attrs={'class': 'watch-view-count'}):\r\n        video_details['NUMBER_OF_VIEWS'] = div.text.strip()\r\n    for button in soup.findAll('button',attrs={'title': 'I like this'}):\r\n        video_details['LIKES'] = button.text.strip()\r\n    for button in soup.findAll('button',attrs={'title': 'I dislike this'}):\r\n        video_details['DISLIKES'] = button.text.strip()\r\n    for span in soup.findAll('span',attrs={'class': 'yt-subscription-button-subscriber-count-branded-horizontal yt-subscriber-count'}):\r\n        video_details['NUMBER_OF_SUBSCRIPTIONS'] = span.text.strip()\r\n    hashtags = []\r\n    for span in soup.findAll('span',attrs={'class': 'standalone-collection-badge-renderer-text'}):\r\n        for a in span.findAll('a',attrs={'class': 'yt-uix-sessionlink'}):\r\n            hashtags.append(a.text.strip())\r\n    video_details['HASH_TAGS'] = hashtags\r\n    with open('output_file.html', 'wb') as file:\r\n        file.write(html)\r\n    today = str(date.today()).replace(\"-\",\"_\")\r\n    counter = 0\r\n    for file in os.listdir():\r\n        if today in file:\r\n            counter += 1\r\n    filename = today + \"_\" + url.split(\"\/\")[-1] + str(counter) + \".json\"\r\n    with open(filename, 'w', encoding='utf8') as outfile:\r\n        json.dump(video_details, outfile, ensure_ascii=False,indent=4)\r\n    print ('----------Extraction of data is complete. Check json file.----------')\r\n    with open(filename, 'r', encoding='utf8') as outfile:\r\n        for line in outfile:\r\n            print(line)\r\n    print(f\"data saved in {filename}\")\r\n    # os.startfile(filename)\r\n\r\ndef check(event):\r\n    global url\r\n    label[\"text\"] = \"Wait... the data will be saved and will appear in the console\"\r\n    url = entry.get()\r\n    get_data()\r\n    root.destroy()\r\n\r\n# Input from user\r\nroot = tk.Tk()\r\nroot.title(\"Youtube Views Tracker\")\r\n# v = tk.StringVar()\r\nlabel = tk.Label(root, text=\"Write the address of the video and press return\")\r\nlabel['font'] = \"Arial 20\"\r\nlabel.pack()\r\nlabel2 = tk.Label(root, text=\"Data will be saved in a file and will appear in the console\")\r\nlabel2['font'] = \"Arial 20\"\r\nlabel2.pack()\r\nentry = tk.Entry(root)\r\nentry.pack(fill=tk.BOTH, expand=1)\r\nentry.bind(\"&lt;Return&gt;\", check)\r\nentry.focus()\r\nroot.mainloop()<\/pre>\n<h4>Utilities<\/h4>\r\n<a href=\"https:\/\/pythonprogramming.altervista.org\/mind-map-with-python\/\">\r\n<img decoding=\"async\" src=\"https:\/\/i1.wp.com\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/PYDOT.png?resize=321%2C229&ssl=1\"><\/a>\r\n<script>\r\nvar title = \"Utility\";\r\nvar links = [Mak\r\n\t\t\t e\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/create-exe-from-python-script-with-images-with-pyinstaller\/\",\"Make Exe cointaining Images\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/send-email-to-different-people-with-python\/\",\"Send More Email at the same time\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/convert-some-data-into-another-format\/\",\"Convert data separation type into another\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/use-sublime-integrated-with-github\/\",\"Github + Sublime text 3\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/make-html-tables-with-pandas\/\",\"Excel to Html Table with Pandas\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/create-a-beautiful-html-table-with-python\/\",\"Beautiful Html Table made with Python\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/split-images-with-pil-aka-pillow-and-python-for-sprite-animation\/\",\"Crop images\"],\r\n\t[\"https:\/\/pythonprogramming.altervista.org\/sublime-text-snippets-how-to-use-them\/\",\"Snippets in Sublime Text\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/create-a-presentation-html-page-with-all-the-images-in-a-folder-in-a-second\/\",\"Create Html page with images using Python in seconds\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/mind-map-with-python\/\",\"Mind Maps & Python (Pydot)\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/pixelize-an-image-with-pil-and-python\/\",\"Pixelize images\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/tkinter-to-make-pdf-fast-and-free-with-text-or-html\/\",\"Create PDF with Tkinter\"],\r\n[\"\",\"Python Lauches a file in Chrome 2\"],\r\n [\"https:\/\/pythonprogramming.altervista.org\/create-an-html-page-and-launch-it-with-cefpython3-in-chrome\/\",\r\n  \"Python launches a local file in Chrome 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-to-powerpoint-2-0\/\",\"Python vs Powerpoint v.2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/what-if-you-got-dns_probe_finished_nxdomain-with-glitch-me-sites\/\",\"Cannot open a site (dns solution)\"],\r\n['https:\/\/pythonprogramming.altervista.org\/convert-text-to-html-docx-and-pdf\/', 'Covert txt to html, docx and pdf'],\r\n['https:\/\/pythonprogramming.altervista.org\/capture-screenshot-with-python-and-lackey\/', 'Capture screenshot'],\r\n['https:\/\/pythonprogramming.altervista.org\/check-if-a-directory-exists-with-python\/', 'Check if a directory exists'],\r\n['https:\/\/pythonprogramming.altervista.org\/how-to-copy-all-the-files-in-a-directory\/', 'Copy all files in a folder'],\r\n['https:\/\/pythonprogramming.altervista.org\/put-some-order-in-my-files-with-python-with-filecollector\/', 'Copy many file on pc in one folder'],\r\n['https:\/\/pythonprogramming.altervista.org\/creare-una-tabella-con-python\/', 'Creare tabelle html con python'],\r\n['https:\/\/pythonprogramming.altervista.org\/make-an-image-with-text-with-python\/', 'Create an image with text'],\r\n['https:\/\/pythonprogramming.altervista.org\/create-an-exe-with-pyinstaller\/', 'Exe from .py (pyinstaller)'],\r\n['https:\/\/pythonprogramming.altervista.org\/create-a-test-in-html-from-a-txt-file\/', 'Format test in html from txt file'],\r\n['https:\/\/pythonprogramming.altervista.org\/freezing-dependencies-hell-on-python-2018\/', 'Freeze dependencies'],\r\n['https:\/\/pythonprogramming.altervista.org\/google-text-to-speech-example-of-gui-to-create-mp3-from-text\/', 'GUI to create MP3 (gtts)'],\r\n['https:\/\/pythonprogramming.altervista.org\/git-basic-commands-to-keep-track-of-your-project\/', 'Git and Github'],\r\n['https:\/\/pythonprogramming.altervista.org\/grab-a-web-page-without-strange-characters\/', 'Grab a web page without strange characters'],\r\n['https:\/\/pythonprogramming.altervista.org\/grab-the-text-from-a-word-document\/', 'Grab text from Word documents'],\r\n['https:\/\/pythonprogramming.altervista.org\/grab-data-text-from-the-web-with-python-and-urllib\/', 'Grab web text'],\r\n['https:\/\/pythonprogramming.altervista.org\/1563-2\/', 'Html Tables with Python'],\r\n['https:\/\/pythonprogramming.altervista.org\/join-all-images-vertically-or-horizontally\/', 'Join Images'],\r\n['https:\/\/pythonprogramming.altervista.org\/repl-with-sublime-keybinding-to-use-python-interactively\/', 'Keybind Sublime'],\r\n['https:\/\/pythonprogramming.altervista.org\/how-to-get-all-the-file-in-a-directory\/', 'List all files in the directory'],\r\n['https:\/\/pythonprogramming.altervista.org\/all-the-files-in-your-hard-drive-with-os-walk\/', \"List all hard drive's files\"],\r\n['https:\/\/pythonprogramming.altervista.org\/transform-a-png-in-a-thumbnail\/', 'Make a thumbnail (PIL)'],\r\n['https:\/\/pythonprogramming.altervista.org\/a-simple-calculator-with-tkinter\/', 'Mini-Calculator'],\r\n['https:\/\/pythonprogramming.altervista.org\/how-to-merge-all-pdf-files-present-in-a-folder\/', 'Pdf merger'],\r\n['https:\/\/pythonprogramming.altervista.org\/tkinter-a-program-start-mp4-with-ffplay\/', 'Play video with ffplay \/ os'],\r\n['https:\/\/pythonprogramming.altervista.org\/png-to-gif\/', 'Png to Gif (PIL)'],\r\n['https:\/\/pythonprogramming.altervista.org\/using-the-re-module-to-grab-numbers-in-a-text-and-make-different-tests-with-few-code\/', 'Practical example with Regex'],\r\n['https:\/\/pythonprogramming.altervista.org\/python-to-make-svg-easier-for-html-pages\/', 'Python and SVG'],\r\n['https:\/\/pythonprogramming.altervista.org\/make-python-use-your-modules\/', 'Python modules finder'],\r\n['https:\/\/pythonprogramming.altervista.org\/how-to-read-and-write-files-in-python\/', 'Read and write files'],\r\n['https:\/\/pythonprogramming.altervista.org\/rename-all-files-in-a-directory-with-python\/', 'Rename all files'],\r\n['https:\/\/pythonprogramming.altervista.org\/find-all-the-files-on-your-computer\/', 'Search all files of a type'],\r\n['https:\/\/pythonprogramming.altervista.org\/split-square-images-into-many-images-with-python-and-image_slicer\/', 'Split images'],\r\n['https:\/\/pythonprogramming.altervista.org\/split-an-image-and-rename-the-output-images\/', 'Split images'],\r\n['https:\/\/pythonprogramming.altervista.org\/python-inserting-subtitles-to-youtube-videos-on-your-wordpress-site\/', 'Subtitles for Youtube'],\r\n['https:\/\/pythonprogramming.altervista.org\/from-text-to-mp3-with-gtts-reading-external-file\/', 'Text to Mp3 (gtts)'],\r\n['https:\/\/pythonprogramming.altervista.org\/if-you-get-an-error-using-gtts-module\/', 'The gtts error'],\r\n['https:\/\/pythonprogramming.altervista.org\/create-a-true-false-test-with-random-order-of-sentences-with-python\/', 'True false test with Python'],\r\n['https:\/\/pythonprogramming.altervista.org\/unzip-and-unrar-to-extract-zipped-files-with-python-and-7zip\/', 'Unzip and Unrar'],\r\n['https:\/\/pythonprogramming.altervista.org\/use-your-own-module-on-python\/', 'Use your own modules'],\r\n['https:\/\/pythonprogramming.altervista.org\/download-a-video-from-youtube\/', 'Youtube video download']\r\n\t\t];\r\n<\/script>\r\n\r\n\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\r\n\r\n\n","protected":false},"excerpt":{"rendered":"See the views of a youtube video with python\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/get-data-from-youtube-videos-with-python-tkinter-gui\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,122,483,49,543],"tags":[4,51,53,669,235],"class_list":["post-4637","post","type-post","status-publish","format-standard","hentry","category-examples","category-gui","category-python","category-tkinter","category-youtube","tag-python","tag-tkinter","tag-video","tag-views","tag-youtube"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=4637"}],"version-history":[{"count":2,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4637\/revisions"}],"predecessor-version":[{"id":4648,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4637\/revisions\/4648"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=4637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=4637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=4637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}