Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
yo what's up YouTube it's been a while
since I made a tutorial but it's great
to be back recently I've been getting a
lot of questions about what projects
should I build to get a job to be honest
there's no one project that will
guarantee that you'll get a job how I
like to look at it is that you should
build something that you're able to talk
about for at least 10 minutes during an
interview for example if you built a
calculator app do you think that you can
talk about it for at least 10 minutes so
I built this calculator app that can add
numbers subtract numbers multiply Etc
it's super cool
um yeah that's about it as you can see
this calculated app wasn't that
interesting I wasn't passionate about it
and I couldn't really talk about it that
much however if you build something that
can solve a real life problem that
you're facing you'll be more inclined to
talk more about it so for today's video
I want to talk about a problem that I've
been facing recently and how I was able
to solve it with python automation
remember folks if you ever find yourself
doing a manual task over and over
there's probably a way that you can
automate it with code so without further
Ado let's get into it so before we touch
any code we need to identify a problem
that we want to solve with automation
for those of you that don't know I hope
my girlfriend run a tick tock account
for our cat papaya you guys should
follow
papayohoe.cat on tick tock
currently if you want to be a content
creator you have to spread yourself out
and repost your content on multiple
platforms so that way you can grow we've
been creating our own content on Tick
Tock but we also want to grow our
presence on YouTube shorts the problem
is we have over 500 Tech talk videos and
ain't nobody got time to manually do
this now moving forward we need to
identify the steps to solve the problem
manually so first I would open up the
tick tock website click the share button
copy the link and use some third-party
websites to download the video without a
watermark and then I would upload it to
YouTube shorts everything I'm doing here
is totally fine but it's very tedious
and cumbersome and it takes a lot of
time so since I know Python and I know
how to code I can automate this and
that's what I'll be showing you guys
today so before you watch this video
make sure you know some python so that
you can follow along if you don't have
any python experience don't worry I have
a playlist over here it's completely
free and top by myself so check it out
without further Ado let's go loading
time
alright so first things first let's open
up Papaya's Tick Tock page and also just
a reminder that this is just an example
so feel free to use any Tick Tock page
that you want to automate downloading
videos for to get the link to the video
all we have to do is open the video and
copy the address at the top this method
requires a lot of clicking so instead we
should think like programmers every
website basically uses HTML in order to
link a user to another page we have to
use an a tag and provide an href with
the URL that we want to link the user to
so to access the HTML on the page all we
have to do is right click and click
inspect and this opens the console tool
so that we can inspect the HTML on the
page and as you move your cursor around
you'll see that it will highlight each
HTML element so for our case we want to
see the a tags for each of these videos
and when I move my mouse here I see that
the stuff that I want is highlighted so
let's expand this so click the arrow to
expand and now as you can see there are
two divs the first one is for videos and
liked and the second one is for all of
the videos so now let's expand this and
let's expand this one and here we go we
got each individual video so now let's
open the first one and let's try to find
the a tag I'm going to expand this
expand this expand this and look at that
we found the a tag and I'm just going to
click on it just to make sure that it
works and this looks like the correct
link so let's close this so now that we
confirmed that this works the next thing
we need to do is find a way to get this
HTML document so that way we can grab
all the hrefs for each video so we can
achieve this very easily by using python
selenium and beautiful soup so now open
a new file and give it a name and save
it and inside here type from selenium
import Webdriver and this will basically
import the selenium Library which
basically allows you to write automation
code that opens a web browser and then
it can click stuff search stuff and Etc
you can kind of think of this as a bot
so cool next just type driver equals
webdriver.chrome which basically
specifies the browser that we want to
use so in this case we want to use
Chrome next type driver.get and then
open the parenthesis and inside here
open quotation marks and paste the link
of the website that you want to get so
in this case we want to get the tick
tock website and next we want to add a
delay to the code so that way it waits
for the website to load so we can do
time dot sleep and then open the
parenthesis and put one for one second
and in order to use time in Python we
have to import time so let's type import
time cool so now that we have loaded the
web page we want to parse the HTML
inside it and a great library for this
is beautiful soup so to use beautiful
soup all we have to do is import it so
we have to type from bs4 import
beautiful soup and now in our code we
can do soup equals beautiful soup and
then open the parenthesis and type
driver Dot Page underscore source which
will give us the HTML Source from the
page and now hit comma space and open
the quotes and inside here type
html.parser to specify that we want to
parse HTML cool now in the next line
just type print open the parenthesis and
do soup Dot prettify and then open the
parenthesis this line will let us test
whether our code works and purify just
makes the HTML look pretty so that it's
easy for us to read now hit save and
save this code somewhere on your
computer and now open your terminal and
vs code in the top you can click
terminal and click new terminal and this
will just open a terminal and to run
this code you can type the command
Python and I'm using python3 so I'll
type three space and then type the the
name of your file so for me it's scrape
underscore video dot Pi if you're not
python on your computer make sure to
install it installing python is out of
the scope for this tutorial so I won't
be covering that and if you run into
errors related to missing dependencies
you can fix that very easily by typing
pip and in my case since I'm using
python3 I have to put a 3 at the end and
then you can just do install and
basically the name of the module so here
you can type selenium and then we want
to install beautiful soup which is bs4
like this and then hit enter and this
will basically install the libraries
that we need to run this code once
everything is set up feel free to run
the code and as you can see it opened a
web browser and then it loads the page
and after one second the page closes
because the code finished executing and
now if you look inside my terminal I
basically got the HTML for the page cool
so now we basically have this HTML
within our python code and the cool
thing about beautiful soup is that it's
able to find elements within the HTML so
for example each video is within a div
with a class called Tick Tock Dash x6y
Etc and they all have the same class
name so basically with beautiful soup we
can specify that we only want divs with
the name Tick Tock Dash x6y Etc and it
will select all of them and return it
back as a list and to make our lives
easier we should try to get the div
closest to the a tag and the div that
contains the a tag is Tick Tock Dash YZ
6i Etc and just be safe we should double
check that all the other divs have the
same naming so let's open the second
video and open the first div and open
this div and open this div as well and
here we see the a tag and now if we look
at the class we see Tick Tock Dash
yz6ijl
Etc which basically matches the one at
the top so this makes our lives very
easy so now we can just look for the div
with this class name so let's copy this
and now let's go back to our code and
now remove this line and type videos
equals soup dot find underscore all and
now open the parenthesis and open the
quotation marks and type div and then
add a comma and then space and then open
this clinical brackets and now inside
here open the quotation marks and type
class and then open the colon and I open
the quotation marks and paste that class
name and all we're doing here is we're
saying we want to find all the divs with
the class name Tick Tock YZ Etc and I'll
do print and let's get the length of the
videos so that way we know how many
videos we got back and then next we can
just Loop through each video so we can
do for video in videos and add a colon
and go to the next line and now just
type prints open the parenthesis and
type video and now we can do Dot and
then we can type a which means it's
getting the a tag within the div and
then open the square brackets and then
open the quotation marks and type href
which means we want to grab the href
value from inside the 8
and now let's hit save and now go back
to your terminal and let's run this
command again cool so it opens the page
and then one second later it closes and
now if you look at my terminal again we
basically got all the links of the
videos inside the page and if you look
closely we only got 29 videos this
doesn't really add up because
technically we actually have around 500
videos so it looks like something's not
working properly so let's go back to
this page so one interesting thing about
this page is that as you scroll more
videos get loaded so this is going to be
a problem for our code because we're
only loading whatever we can see
initially so if we want to see more
videos we have to tell selenium to
scroll through the page until it reaches
the bottom so that way we can get all of
the videos now that sounds super
complicated and that's where Google
comes in we should use Google as a
resource to help us find a way to
achieve this so I came across this
medium post by Quan Wei and this does
exactly what we want to do here's the
code that basically handles scrolling
all the way to the bottom of the page so
we only care about this part of the code
so let's copy it and let's go back to
our code and let's paste this in the
middle before we grab the page Source
from the driver and I'm missing an S
here so let's add it back so basically
this code just runs a while loop that
executes JavaScript code to scroll down
and then we have a variable I to keep
track of how many times we have scrolled
and here we tell the program to sleep
for scroll pause time which is just one
second and here we execute another
JavaScript function which basically
grabs the new scroll height of the page
and we basically compare the height of
the screen multiplied by the number of
times that we have scrolled and we check
whether this value is greater than the
current scroll height and if it's
greater that basically just means that
we've reached the end of the page cool
so now let's hit save and now let's run
our code again hit enter and here it
opens the browser and let's watch some
magic
you see that it's scrolling by itself
isn't that awesome and now I'll just
speed up the video and let it scroll all
the way to the bottom five minutes later
and that took a while because we had a
lot of videos but now look at our
console we actually have a lot of videos
and when I scroll all the way to the top
you're gonna see that we have 457 videos
in total I guess I was off by like 50
but still that's a lot of videos cool
now that we have all these links the
next thing we need to do is just go to
this website
ssstick.io which basically allows you to
download Tick Tock videos without a
watermark so let's paste the link to one
of the videos and before we hit download
right click on the page and go to
inspect and then go to the networks Tab
and basically on this tab it will record
network activity so let me show you what
that means so let's go back to the page
and click download and if you look at
the right you're going to see that three
Network activities happened when we
click the download button and if you
click response you're gonna see that it
returns HTML and if you look here
there's an a tag and if I scroll to the
right and you're going to see that this
a tag is for this fun without Watermark
so basically we only care about this
href from this a tag and if you right
click on this network activity go to
copy and then click copy as curl go to
the site called curled converter.com and
inside this text field just paste what
we copied and this will turn the curl
command into python so now scroll to the
bottom and click copy to clipboard and
now let's go back to your code and let's
create a function and call it download
video and open the parenthesis and here
it can take a link as a parameter and
open the colon and hit enter and now
paste the code that we just copied and
now let's move this import to the top of
the file so delete this line and scroll
to the top and let's paste it on line
four and let's fix the spacing and hit
enter and now scroll down and now let's
indent all the code that we just copied
and now let's fix the spacing so
basically all the code here just
represents the data that gets sent over
when we make the request to that website
and if you scroll all the way to the
bottom you're going to see that we make
a request to post data to this link and
we pass in the parameters and the
cookies headers and the data and if you
look in the data field you're going to
see this ID which has a link to the tick
tock video so instead of hard coding a
link let's replace this with link which
is the parameter that we added to this
function so now let's copy the function
name and here instead of printing this
value let's just call our download video
function and now let's go back to the
bottom of the page and after we make
this request we'll get back a response
and since the response is HTML all we
got to do is just use beautiful soup to
parse the HTML data so now let's create
a new variable called download soup and
this will equal beautiful soup open the
parenthesis and type response and if we
dot text this will give us the HTML
inside the response and put a comma and
open the quotation marks and type HTML
dot parser and then hit enter and like I
mentioned before we only care about the
first a tag so we can get that very
easily so let's do download link equals
download soup dot a square bracket href
and now the last thing that we need to
do is to download the file so we have to
import another Library so let's scroll
to the top and now in line five just
type from URL lib dot request import URL
open and hit enter and basically we will
use this library to allow us to download
the raw data of the file so now let's go
back to the bottom of the page and after
the download link let's create a new
variable called mp4 file equals URL open
and then we can pass it the download
link and this will download the file as
raw data and now all we have to do is
save this file onto our computer so
let's do with open
and then open the parenthesis and let's
use an F string here so F quotation
marks and I want to put it in a folder
called videos slash and open this Google
brackets and let's put the ID of the
video here and then we can do dot MP4
and then add a comma and open the
quotation marks WB which stands for
writing in binary so we need this in
order to write to a file and then type
as output and then put a colon and hit
enter and now we're going to write a
while loop so while true all we're going
to do is data equals mp4 file dot read
4096 so now let's get back to the code
so hit enter and now type if data so
basically if we're able to read data we
want to write this data to the output
file so do output dot right and then
open the parenthesis and put data and
then hit enter and then add an else
statement where basically if no data
comes back that just means that we
finish reading from the file so now we
can add a break statement which means
that we're done with the wow so now hit
save and before you run the code make
sure you make a folder called videos in
the folder where your script is located
because in the example here I'm creating
the video inside of folder called videos
and I'm giving it an ID and that just
reminded me I forgot to pass a ID
parameter so let's scroll up and and
here inside the download video add a
comma and put ID here and in here let's
just pass the index as the ID so now if
we want the index all we have to do is
add index here and add a comma and here
on the videos we do enumerate and open
the parenthesis and close the
parenthesis and this will basically give
us the index along with the video and
now let's hit save and now let's run our
script cool so it opens the page and now
it Scrolls to the bottom and yeah of
course I got an error live coding is
just too hard so basically the error is
saying that download video is not
defined so let's look at my code and I'm
assuming the error is because I declared
the function after it was called so
that's why the code can't find it so we
can fix this very easily so let's copy
this whole function and let's delete it
and now scroll to the top of the video
and let's paste it above everything else
and hit save and let's try this again oh
crap looks like I got another error I
just had a spelling mistake so I was
supposed to capitalize this F so let me
do that and let me try this again all
right let's cross our fingers and hope
that everything works fine
ah crap we got another error hmm it
looks like our script wasn't able to
read the href inside the a tag but on
the bright side it looks like we did
download one video so let's open this
video and make sure that it works
nice it looks like it works
but technically we're supposed to
download all the videos right and the
issue here is that we're spamming a
server with a lot of requests very
quickly so the server probably thinks
that we're bought and we can actually
get around this very easily by just
adding an arbitrary DeLay So now let's
scroll down and go to where we called
the download video so right here so
after we download one video Let's do
time dot sleep and let's just sleep for
10 seconds just be safe so let's save
this and let's run the code again
third time's the charm right crossing my
fingers let's go and look at that we got
zero and one
and let's wait another 10 seconds and
you're gonna see the next video get
downloaded and look the third video just
came in so because there's a 10 second
delay it's gonna be a long while before
we download all the videos but basically
look it's working and this is awesome
and unfortunately YouTube has a cap and
I can't upload more than 10 videos at a
time so I guess what we have right now
is pretty good anyways that's it for
this video I hope that you guys learned
something new I wanted to share my
thought process of how I approach this
problem and how I was able to come up
with the solution as you can see we only
scratched the surface of automation
there is so many things that you guys
can do if you ever get stuck try to read
the documentation for beautiful soup and
also selenium and if you guys are up for
the challenge try to build your own web
scraper to grab some useful data maybe
you guys can even build a sneaker bot or
even some application that will notify
you when an item that you want to buy
goes on sale anyways the world is your
oyster so try to build something cool
and let me know what you guys built in
the comments below thank you guys I'll
see you later
[Music]
[Music]
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.