Scrap items.
Hello, everyone.
Today, we're going to talk about scrapping items and first of all, before jumping into the coding,
let's talk about what item is so usually in scrap when you create a spider, which you're already familiar
with.
Sometimes the conversion from the HTML to item luxon structure, it is not much structured unless we
define it.
It is quite easy to make typo in the name and to return very incorrect data.
And this is especially true if you work with quite large projects, which I hope you will work at some
point.
So in order for scrapie to define a common output data Skrappy's using items.
So items are container's simply used to collect the scrap data from the web.
So they'll provide you with directory.
And they they're quite convenient because their syntax is quite easy to grasp.
And you see this in a second.
So here are some examples how you can use items in Python.
So the first thing I will do is I will actually go to item mode.
So let's enter the python mode.
OK.
And and the first thing that we need to do is to actually create a class.
OK, so here we are going to define the different fields that we are going to work with later on.
And you can write import scrapie.
OK, and then let's write class products or products actually.
Product.
Scrapie Dot Eitam.
OK, and then let's right to name equals.
Scrappy dot field, that's it, and after that, let's try price equals to scrappy dot field as well.
And then let's try stock equals to scrapie dot fields, OK?
And it is very important that you go to the exact same indentation level as me, because otherwise a
program will not accept your code as Python is an intentional level language.
So please make sure that the code looks exactly like mine anyways.
Let's do our last.
Up to date it.
Updated.
That's it equals.
Scrappy dot field and then serializer.
OK, you quote a star for string.
OK, that's it.
So we're ready here and our press center.
We have created a class and now since we created the class, we can actually create our first product.
So you saw that in the class?
We specified the metadata for each of the fields.
OK, so now let's create a product and then we'll do product.
OK, equals to product.
So here we create an instance of the product class and then let's right name equals desktop PC price
equals one thousand.
OK.
OK, we've got an error here.
Well this is because in the class instead of price I wrote prints so we'll have to use it that way.
And now we're not getting any errors.
So for the people.
So if now I write a print product.
OK, actually, let me put it in parenthesis product, OK?
You can see that our product name is the same as we named it and the price is one thousand dollars.
OK, so this is basically our content here on third container.
Anyways, let's now actually choose to display only the product name and how we do that simply by writing
product and in the brackets name.
OK.
And if it's like that, I get a desktop issue and if I do a product, not get.
And again, name.
I will get the same thing.
They stop --, so if I do on their side product in here, price it actually with em.
So here is a product price and then we'll get one thousand, which is expected.
And also you can see when it was last updated.
So let's try it out.
So if I do products product and then they are in the quotes last.
Updated, we should get an error here.
Let's see actual updates.
Let's try now product.
Not get in here, I can write last so if the right product.
Not to get.
And here for right last.
Updated.
Not set, you want the program to say, if we don't have the last updated to do not return on error,
but to return us not set.
So if I run this instead of the error, we can see this time they are not that message.
So this is a very good way to display a message to the user instead of an error that is not understandable.
So we can do the same in our way if we do a product and then we do.
La la, la, la, for example.
Obviously, since we don't have this product, we get the care and it says that we actually don't have
this type of key, you know, but instead, actually what we can do here is do product and then get
actually put it in parenthesis, get como unknown, unknown field.
OK.
And when they run that, he actually me fix this, so product actually here is a product that I get
in here and said I will write Lalalala.
OK, so you can see that if we don't have the lalala, we actually get the unknown error, which is
basically the name of the whole exercise.
So also we can check if certain names are actual products inside our field.
So, you know, in our fields actually go back to the field so you can see them in our field.
So we've got name prints and stock.
So, for example, if I write name.
OK, in product.
We will get through.
But for example, if I write lalala in product, we will get false because this is actually not in our
name, said, OK.
So now let's learn how you can set fuel values, for example, if a do product.
And then I actually want to out to the field, so I do the last updated equals to the.
This is now valid.
So now we created the fuel to last updated and we did to be equal to our value today.
So if we do now product and then they're right last.
Of the eight.
OK.
I obviously get the volume today, so this is how you can set certain fields.
OK.
The next thing I want to show you is how to assess all the populated values.
So, for example, here, obviously you can see that we have the name and we have the price.
So the populated values are price and name.
So if I want to check only the populated values, I can do products that keys.
And after a ride that I can see that the populated virus or name and price.
But also we get last updated because we just started it and we added a value to it equal to today.
OK.
And, for example, for a right product.
Those items.
OK.
The items will actually get.
All the dictionary of our products, so we get lots of data today, name desktop PC and we also get
the value of the price.
So we get all the values that we need into this dictionary here.
So this is a good way to obtain them.
Let's check some additional things we can do with items.
So, for example, if I want to create a new product from the previous product, I can do product to
equals to.
Product.
And then here at products.
That's it.
And now will create the new product, which is product from product one, and should be absolutely the
same.
So if I print here product to OK, I will basically get the same thing as product one.
So I will get absolutely the same values because this is a copy.
Also, there there's no way you can copy actually items here by doing.
Products.
Through, for example, could be called to product to not copy.
OK, and here, if I do print product three.
You can see that the product treats a direct copy of product, too.
So there are two ways to print this and it does matter which one you use.
One of them is by using the last call and the other one is simply by using the common.
Both of them basically work in exactly the same way.
OK, and the final thing I want to show you is how to create items from dictionaries.
So let's do now product.
So product and here I will define a name.
OK.
Then the name will be Laptop PC.
OK, so this will be the name, then the price.
Is going to be one thousand five hundred.
OK, so I think here we've got honorable guys just because I don't add parentheses around the name.
So let me do that right now and around price at it.
And also, you remember that I so I have to repeat the same typo here as I created the class or it was
a typo.
But if I hit enter here, you can see that we actually created an item just from the dictionary that
were created here.
And dictionary usually has the key and item here and the name is the key and diatom is A laptop, B,
the same with the price.
So this is all about items that they wanted to share with you.
This is how you create and actually collect items.
So that's, I think, very much for watching, for watching my video.
And I will see you in the next one.