Placeholder Image

字幕列表 影片播放

  • Hello, everybody.

  • I am super excited for this project because we're gonna be building an Amazon price checker which will automatically e mail you any time.

  • Any product you want goes below a certain price.

  • So you tell it what products you want, what price she wanted to be below.

  • And I will email you as soon as the price drops below that price point.

  • That sounds amazing.

  • And it's actually really simple.

  • The set up and I'm gonna show you how to do it starting now.

  • Welcome back to Webb.

  • Have simplified my name's Kyle.

  • And my job is to simplify the web for you so you can start building your dream project quicker.

  • And if that sounds interesting, make sure you subscribe to the channel for more videos just like this one.

  • As always, I have visual studio code opened to a completely blank folder so we can get started from scratch.

  • And we're gonna be using no to create a simple job script application that we can run.

  • So we could just set this up by typing at 10 p.m. On it and Haiti enter.

  • And if we just hit, enter a bunch of times, don't give us all the default values.

  • And as you can see, we now have a packaged out Jason which contains all the different dependencies for our project.

  • But we haven't added any dependencies yet, so it's actually add the dependencies we're gonna need and for our application, what we need to do is first, we need to go to the Amazon website and find the price.

  • So we need to do some form of Web scraping.

  • So we need a library for that.

  • We also need to go and send out an email.

  • So we need a library for handling an email and then lastly, we just need the library to handle environment variables because our E mail service is gonna have an A p i ke that we need to send it.

  • So to install those, we can type N p m I.

  • And the very first thing that we need to install is gonna be called Nightmare and Nightmare will essentially allow us to actually do the Web scraping from amazon dot com.

  • It will actually go to amazon dot com and give us back all of the HTML which we can parse and do what we want with.

  • And the great thing about nightmare is that actually works for websites that are rendered using front and frameworks For JavaScript and amazon dot com is one of those websites.

  • It actually is rendered on the front end.

  • So we need to make sure we use nightmare or some other part, sir, that'll handle that situation.

  • Next, we need to worry about our email on.

  • We're gonna be using centigrade for our email so we can say at send grid slash male.

  • And this is a library that send grid has that allows us to really easily send and receive e mails.

  • And all we're gonna be worrying about is sending e mails and this one we can enter to install those.

  • It'll take just a little bit of time to install these and then the show instead of our Node Modules folder, and I'll be back when that finishes.

  • And that just finished downloading everything for us.

  • Now we have one more library we need to install.

  • This is going to be called dot he envy, and this just allows us to set up environment variables using a dot N V file.

  • So it's create that now dot envy.

  • And inside of here is where we're gonna put our A p i ke that we get from send grid for sending her emails.

  • We're gonna do that towards the end of the video because right now, we actually need to set up and work on actually parsing the HTML from amazon dot com.

  • So was just really quickly create a file.

  • Where is gonna call this part, sir dot Js And this is going to be the file that actually checks the price on Amazon and does all the parson and e mailing for us.

  • And the very first thing we need to do is actually find out what element we want to get from the HT Mohan Amazon.

  • So I just have a simple Amazon pricing listed over here for a one terabyte solid state drive and we see right here this is the element we want to get.

  • We want to get this price.

  • We need to figure out some way to select to that price and a really easy way to do that is to just right click hit, inspect, and from here were actually able to look at the exact HTML code from the page, and all we need to do is click this little tool up here, which allows us to select an element inside of Paige.

  • And as you can see, when we hover over this, it is the actual element we want.

  • This is the price we click on it to select it.

  • And then over here, you see, we have this span with the I d of price block our price, and you see, it has a string $169.99.

  • We need to actually make sure we keep in mind this identify because this is gonna be the i d.

  • That actually gives us this piece of information when we set up and use nightmare.

  • So I'm just gonna copy this over just so we have this inside of our application to work with and I just pasted in a string at the top of our page for now.

  • And now that we have that out of the way, let's just maximize this screen.

  • So we have our entire visual studio code to work with, and the very first thing we need to do is just set up a basic function for checking the price.

  • We're gonna make this an a sink function because we're gonna do some asynchronous code in here so we can say basic function.

  • We just want to call it check Price and then inside of here, we're actually going to check the price of our element using nightmare.

  • So the first thing we need to do to get working with nightmare is to actually require nightmare.

  • So let's go up here.

  • We could just say constant nightmare is going to be equal to require and we want to require nightmare.

  • And this is actually just returning to us a function.

  • So we could just call that function by putting extra parentheses at the end.

  • And this is going to be our nightmare object.

  • We can pass in configuration values here if we need to, but in our case, all the defaults are just fine.

  • So now that we have that set up, we could just come in here and say nightmare dot go to and what this allows us to do is go to some euro.

  • So if we go back to our Amazon example, we can copy over this euro.

  • Let's just make sure we paste this in here.

  • So we have it to work with.

  • And what we want to do is we're gonna remove all of the extra information after the path.

  • As you can see, the final path is this B 07 bien.

  • Everything here afterwards is stuff we don't need, so it's actually just remove all of this.

  • This is just tracking information that Amazon uses to keep track of everything.

  • So we don't actually that we just need this single girl here, which we could just paste inside of here.

  • Remove that.

  • So now what we're doing is we're telling Nightmare to go to this euro and actually download the information from the page.

  • Once it's downloaded from the page, we can use what's called dot wait, and this dot Wait, command essentially allows us to wait until some element is rendered on the page.

  • This is the part that allows nightmare to work with something like react because react.

  • What will happen is that we'll get the HTML and the HTML is gonna be completely blank once the page loads and then the Java script will run.

  • Tau actually render the html.

  • So we want to do is we just want to wait till our price block Our price is showing up.

  • And if you remember right, that was an I d.

  • So we just put in here a CSS selector.

  • In our case, we're waiting for the element with the i d of this I d price block our price.

  • And once that shows up, we know our page has loaded and the price we want is on the page, so we can actually run some code on the document in order to get this price.

  • And to do that, we need the use what's called evaluate.

  • So the evaluate takes just a single function.

  • So there's a function inside of here, and this function allows you to essentially right javascript as if you were on the front end of that application.

  • So we have access to the document, for example, we could just say document get element by I d.

  • And we can get an element with that price block our price, I d.

  • So it's copy that in here.

  • Now, what we've done is we've gone to that euro.

  • We've waited until our element is shown on the page, and then we're evaluating some code to say give me the element with that, I d And we just want to get the text inside of it and so will say in our text.

  • So now what we're doing is we're actually getting the text from inside of that element on the HTML page on Amazon's Web site at this euro.

  • And now that we know that that's all that we want to do, we could just use dot end and this is actually going to end this segment of code right here.

  • And it returns us a variable, which is what we evaluated right here.

  • So we can just say we want to get the price String is what we'll call our variable.

  • We need to make sure that we actually await nightmare because this is all a synchronous.

  • So we need to make sure we put a weight here, so that is actually waiting that code and then running everything over here and I'm just gonna tidy this code up just a little bit, so it's easier to read.

  • We're gonna move this all the way to the end here, just line everything up.

  • There we go.

  • And now that we have a press string, we can move on to actually parsing the string into a particular price.

  • So if we could just come down here, we can say we want a price number which is going to be equal to our price string.

  • And all we want to do is replace the dollar sign because as you remember, there's a dollar sign inside of this string.

  • So we just want to replace the dollar sign with nothing.

  • And then, of course, make sure that we turn this into a number.

  • So we'll say parse float of that.

  • So now what we're doing is we're replacing the dollar signs that we have just a single number and then we're converting that string into a number and storing it in this price number variable.

  • Now we can do our check for what price we want.

  • Let's say, for example, if we remember and go over here exit out of this, we saw that the price was $169.

  • Let's say we want to get notified when the prices below 200 so in this case we would actually get notified.

  • Let's go over to our code and we can say if the price number is less than 200 which is our number we want to get notified for.

  • Then we could just run some code for now.

  • We'll just counseled out Log it is, Chief.

  • Otherwise, if, for example, it is not she had met, we can just say another log and just say it is expensive.

  • There we go.

  • And now that's all the code that we actually need.

  • What's just come in here and make sure you run that function so we'll just say check Price.

  • And if we save this and run node parse er dot Js is actually going to run the code inside of here, and it's going to either print out.

  • It is cheap or it is expensive and it takes just a little bit of time because, as you remember, nightmares actually going out, getting the oral, downloading all the data, waiting for this to render evaluated.

  • It does a lot of stuff, so it takes a little bit of time, but as you can see, we get a message printed out saying it is cheap.

  • But if we change this toe 1 50 for example, and as you remember, over here, the price is 1 69 We should get it saying that it is expensive because it's actually over the price that we want to pay.

  • So if we just give it a couple seconds here, as you can see, it says it is expensive.

  • So not what we have successfully done is we've parsed Amazons pages, toe actually print out whether or not the prices below or above the price that we want to target.

  • But something that we haven't done is this is very static.

  • For example, this you are Oh, we can never change this price.

  • We can never change.

  • I'm guessing you're going to want to change these things in your application.

  • And to do that, we can actually use arguments.

  • So, for example, in here we could pass the euro and we could pass the price.

  • And that way we can actually determine what are your Ellis and what our prices, Every single time we run the function, all we need to actually do is go up here, and every single note application has something called process dot Argov.

  • And this is the argument you pass in and it's going to be an array.

  • The very first argument is node, which is what we're calling.

  • Second argument is the file we're calling.

  • And then everything after that is going to be anything we pass in here.

  • So, for example, you are l would be the third element in that array.

  • So what we want to do is we want to slice that array and we just want to get everything after the second element and beyond.

  • So number two would be the third element.

  • So we're saying, Skip node, skip Parcel dodges and give us anything afterwards, which, in our case, is gonna be our euro in our price.

  • We could just set this here, tow our arguments, and then what we can do is we can get our euro by saying that that's going to be our first argument.

  • So argument of zero.

  • Let's just call These are eggs, makes it a lot easier.

  • And then we can come in here and we'd get the minimum price.

  • This is the price that something must be less than or equal to in order for us to want to purchase it.

  • And it's the second thing that we're going to pass to our function down here and now we can actually use this information in here.

  • Instead of passing this string, we could just say go to euro.

  • And instead of checking a static price, we can say men price.

  • So now let's make sure we copy this euro just like this pace that in there.

  • So we have our euro and our price of 1 50 Make sure code is cleaned up.

  • And now we run this.

  • We should get it saying that it is too expensive because it's gonna go to the shore.

  • Oh, it's going to realize the prices above 1 50 it's going to console log here.

  • And as you saw it, did that.

  • And all we need to do is just changes to 200.

  • Now we know our prices glow 200 so we should get returned.

  • Saint.

  • It is cheap because it's lower than the price we specified.

  • And there you go.

  • You can see that's working, and this is going to work with any Amazon euro and any price that you want to specify.

  • And every time you run it, it's going to check the live numbers on Amazon site and return to you whether or not it's above or below that price every single time.

  • Now, in order to actually start sending e mails, what we need to do is go to send grids website and we just need to create an account and log in which I have already done.

  • And the great thing about send Grids website is that you can actually send a ton of e mails every month completely for free as long as you stay under the threshold.

  • I want to say it's either 500 or 5000 emails a month, which if you're using this simple application, you're never going to surpass that free limit that they give you, which is absolutely great and all you need to do in order to find your AP ikey.

  • You just need to go down to settings and you can see there's a P I keys right here on.

  • What you want to do is you want to create an AP I ke that has access to send mail and our case I've created this a P I Q.

  • Right here.

  • Let's just create a brand new a peaky we can say that we wanted to have restricted access.

  • If we scroll down, you should see mail send, and all we want to do is build a let them send mail.

  • So we just bring this over to here and you can see now has only access to send mail.

  • We can say create interview.

  • Let's make sure we give it a name.

  • We'll just call it.

  • Here are Amazon Price Checker and make sure we click creating view and you can see it's going to print out a P.

  • I Q.

  • Here, and this is something that you want to make sure that you keep secret.

  • I'm going to be removing this ap I ke after I'm done with this video so you won't be able to use this one.

  • But when you create your a P I i ke, it must be kept secret.

  • Make sure you don't commit to any get help repositories or send it out inside the public anywhere where people could access this.

  • So now that we have that copied, let's go back over into our code and in our environment variable, we can create a variable which we wanted to call send grid ap I ke, and we can set that equal to the a p i ke that we just copied, which we can see is here now, with that out of the way, we could go back to our part, sir, And we can actually import that environment variable.

  • And that's what that Dottie envy libraries for.

  • We could just say require dot envy.

  • We want to say dot config just like this.

  • Make sure you put this inside of quotes.

  • There we go.

  • And what this does is it actually loads all of our environment variables into the process.

  • Dottie Envy variable so we can say it straight.

  • I'm sorry, not striped Send grid A p I key just like that.

  • And that's going to be our environment.

  • Variables that we set in our dot n v file here and we want to do is we want to actually set up and hook up that library reimported earlier, which is send grid so we can say creative variable called send grid male s G mail.

  • We're gonna set that equal to require and we want to require at send grid slash male just like this.

  • Now, from here, all we need to do is call send grid mail that set a p i ke and we want to pass it.

  • This a peaky.

  • What this is going to do is going to set our A P i ke for all of the requests that we make to this application.

  • Now we can create a function which, like she send our email.

  • So it's a function.

  • Send email and we want to send an email with the subject, and we wanna have some form of body to our email.

  • Now, what we need to do to credit email is to credit object.

  • We just call it E mail here, and this object is going to have all of our important email fields.

  • For example, we're gonna have a two field.

  • And in our case, what I've done is I've just gone to some fake euro fake email sites called 10 email dot or GE, and it'll just give you a temporary email you can use for testing purposes.

  • I'm just gonna copy this down pasted in here so we can actually see the e mails show up inside of this inbox when we actually send them next.

  • We need to set from field.

  • You just set this whatever you want.

  • I'm gonna call it a Mazon price checker at example dot com This doesn't really matter from the from field.

  • You can just do whatever you want.

  • You can use your own email, whatever email you want next.

  • Our subject.

  • Obviously we're getting this passed in, so we'll just say subject and text.

  • We're gonna put our body and then we also html, which we're gonna set to our body.

  • And the weight email works is you can have both an html email and a text email, and in our case, they're both gonna be exactly the same.

  • So we're just gonna send both of them as exactly the same information for body on both of them.

  • And now all we need to do is send that email so we can just come down here.

  • We can return, lifts return, send grid mail dot send and we want to pass it.

  • That email object And the reason we're returning this is because it's actually returns a promise.

  • It's asynchronous.

  • So we need to make sure that we await that inside of our code.

  • Now, up here, where we can do is we can actually call that send email function, and we want a first pass.

  • It a subject which, speaking, say price is low and then we want to pass it some kind of message, and I'm actually gonna pass it a really useful message that will allow you to view the website from the email.

  • So instead of some string interpretation, we can say the price on and we can pass it.

  • The euro has drops below our price.

  • So essentially, what this is saying is that the price on whatever euro has dropped below the given price that we specified.

  • And in our case, this is actually men Price just like that.

  • And we can't move our consul that wog.

  • And we also don't really need an l statement because we don't want to send an email.

  • If the price is not below what we want, we only want to be notified when the price is actually lower than it should be.

  • So let's just remove this, save it and actually test to make sure this works.

  • As we know our prices below 200 somewhere we run this.

  • We should hopefully see an email show up over here telling us the price is low and it should give us the link to the euro as well is the price that it is lower than so.

  • Let's make sure that works.

  • We can see our application ran.

  • And if we come over here, click Refresh.

  • You can see that we got an email showing up in our in box, and this email has both the link that we want, as well as saying that the price has dropped below $200.

  • Now, one thing that could happen with our application as it could throw some kind of error inside of here.

  • And if this is just running in the background job somewhere, we're not gonna actually notice when this era curse.

  • So we want to actually send an email to ourselves every time in error occurs, and this is easier than it sounds.

  • Well, we need to do is make sure we wrap all of our code inside of a try catch.

  • So what's copy all the code that we have currently and put it inside the tribe portion?

  • Just want to make sure that we fixed the indentation of everything, and that's all lined up there and dental this.

  • Okay, there we go.

  • So now Oliver code is in a try, which means that if our code ever fails, it's going to go into this catch section and in the catch.

  • What we can do is we actually send ourselves an email so we can say send email and we can say Amazon Price checker error.

  • And we want to send the message of that air as the body subject.

  • And then, lastly, just to make sure that we actually get some good longs for the air is we want to make sure we throw that air again.

  • This just means if we go and check the logs for application when it's running, well, actually, see the error of show up here and what I'm gonna do to demonstrate this is I'm gonna purposely thrown error.

  • I'm just gonna put some bogus code in here.

  • This is going to throw an error on us.

  • So if we run this, we should get an air right here.

  • As you can see, it printed out our error right here since we threw that error.

  • And if we go back to our email, make sure we click refresh.

  • We should see another email in here saying that we had an heir, and as you can see right here review this email.

  • It says that we had an heir and it told us the message, which is that that Random Jubilee just that I had typed out is not to find which is correct, the last important thing that we need to make sure we do.

  • Just that we actually await the sending of our email just like this.

  • So that way, nothing wonky goes wrong and that our code exits before e mails actually sent.

  • We just make sure that our email is always going to be sent for us, and that's all there is.

  • Toe Web scraping, sending e mails and checking Amazon prices.

  • If you enjoyed this video, make sure you check on my other videos linked over here and subscribe to the channel for more videos where I simplify the Web for you.

Hello, everybody.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

構建一個應用程序來跟蹤亞馬遜的價格 (Build An App To Track Amazon's Prices)

  • 2 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字