Hi, everyone, and welcome back to part two of our practical challenge or our solution to our practical
challenge.
Now, you'll notice I did clean this file up.
I had some information up here and my apologies when I'm going to work across multiple files to grab
header information, user age information.
I had some extra information pasted in from that.
So I did some cleaning.
But we're going to jump right into this.
And I wanted to mention in the last lecture, we didn't use any XPath information.
We didn't use any selectors.
And that's kind of the benefit with beautiful soup in this case to be able to pass the text, the response
text that we're getting from the page because the page might change in this scenario.
The page might have the text in different informations.
We might not know the XPath, we might not know the specifics.
So if we can pass it, look for the string, the key based string and parse it, that's going to make
our lives much easier.
And it's pretty cool the fact that we can do this with only a few lines of code.
But now what we want to do is use SMTP, Leyb to be able to send and generate an email for our alert.
That's going to help us.
It's going to bring awareness to it.
So let's jump right in and solve this.
In order to do this, we can work right in our here.
We had our print alert statement, which is fine, but we can work right in here because this is what
is going to be run if we find the key.
So if we find the key, we first want to create an email so simple enough we can use create.
We don't want caps.
We can use, create email equal to and let's pass in the subject.
That's what it's going to generate.
It's just going to generate the email and let's put check.
Pastebin found key just to bring our awareness that alert capital letters.
All right.
So that's going to be our subject.
And that's only thing that's going to be sent is just the subject line.
You can use SMTP combined with other libraries to add attachments, but for our purpose, this is enough
just to raise that alert that we're scraping next.
If we're creating an email, we need to generate it from somewhere.
So it would be from an address equal to from address.
That's where you would paste in your address here.
And then you would want to use the address, the to address where the email is being sent.
You would want to address and this is where the email is going.
So you want to send it.
For example, in my case I put my email and I'm sending it to myself so we can use from and to address
to create that email.
All right.
And now we have to actually import SMTP and send it to the SMTP, live Gmail dot com to use Port 587,
because this is the default email submission port.
So when an email client, outgoing server is submitting an email to be routed, it's going to be using
SMTP Port five seven.
One thing I also want to note, I highly recommend setting up a test Gmail for this purpose because
we're not using SSL here.
I highly recommend using SSL and it's possible to use it with SMTP, but for the purpose.
We're only testing here for our use case scenario.
But if possible, use SSL, especially when you're working with password information or email information.
It's always better to use secure information or SSL in this case will be the secure sockets layer.
But lets pass in our mail here equal to SMTP, Leyb, SMTP and we can set it to use SMTP, Gmail dot
com and we have our port five eight seven.
Just going to add a no use SSL recommended.
All right, and also, if you run into an error when you're using this from the SMTP lab, because we're
using Gmail, Gmail has a security feature for apps that are deemed less than secure.
And this is going to throw that error.
So if you are using and testing this, you can go to your Gmail settings.
You can enable this.
But I highly recommend turning it back off when you're not testing this.
So one, please, if you can set up a new test, Gmail, too, if you do test this, turn it back off
to disable because you do not want apps or settings that are allowed to access your email that are less
than secure.
This is only for testing purposes, for the purpose of the scenario so we can see how to set up an email.
So once we have our port enabled our Port 587, we can start.
So it's simple enough.
Let's use mail dot start deals, OK?
And we can set our username and password so we will need to login login with our username and password
and I'm not going to include the credentials.
And please, if you upload this in the next lecture to GitHub with the code for feedback, please remove
your username, password and the email address.
You never want to include that sensitive information.
So I will drill down on this.
Please remove the sensitive information from your Python file.
Do not include it.
Moving on, we can use our login and we want to send email with our parameters that we've already specified
so we can do mail send no, not email here, that was almost a little subconscious because we're sending
an email, but we want to send mail and we can use what we have created here.
So we want to send our firm address.
To address and to create email.
And then lastly, we don't want to keep this open, so we want to use mail to close close that up.
I'm also going to include our print statement just from earlier since I deleted it by accident.
And we'll have our break.
Now, this is going to create that email.
I'm just going to close this for some spacing.
All right.
We have that.
And this is how you create email.
I'm going to pause the video and I will bring up a screenshot of the email created so you can see what
it looks like.
I'm not going to run this because I don't want to pass in any of my username information and sensitive
information.
So one more time, please do not upload any type of sensitive information in the next lecture when you
can submit it to GitHub for feedback.
This is such a simple and fascinating way to set an alert if we find that information is really cool.
Scraping tool.
I hope you guys are enjoying it.
And let me pause video now and bring up that screen shot, and here we have that screen shot so we can
see we had a success.
Congratulations.
Awesome work, guys.
Hope you're taking away a lot of useful information from this.
Always remember here we can use XPath information, you can use scrapie and scrapie actually has a built
in email method, too, which is really powerful.
The main reason I use this method for describing purposes was to pass the text and search the text instead
of using a specific class selector or XPath related method of obtaining that.
OK.
I definitely feel like I need to grab some coffee right now.
Take a quick break and we'll jump back in the next video.
I'll show you guys how you can upload your Python file if you use any other different methods of approaching
this.
If you customize it further, if you want to try and get some feedback on your scraping approach, I
will see you in the next lecture.