Whatever your project size is, we will handle it well with all the standards fulfilled! We are here to give 100% satisfaction.
Data has become a mighty weapon that can influence the direction of this world. This can decide the subsequent actions, which need to consider increasing sales by offering products related to customers’ tastes, using Artificial Intelligence to minimize human work, and more.
This blog will show how to scrape data from a current website; the action is generally called data scraping. For that one, we would use Tokopedia, an Indonesian E-Commerce website.
The initial step to data scraping is deciding which data we wish to get. Here, we need to find shoe (sepatu) data, and this would be organized by a review (ulasan).
Let’s observe the site. This website is formed by the markup language called HTML. And we could get the data that we want by searching the HTML of a page carefully.
Initially, let’s open the Tokopedia page at https://www.tokopedia.com.
Let’s search for the shoes on a search bar. In Indonesia, shoes are known as “sepatu”, so we will use a word “sepatu” in a search bar.
However, it’s organized by the most appropriate one, therefore, let’s change it to category by a review by changing a dropdown “Urutkan” to “Ulasan”.
Let’s observe an HTML by utilizing inspect elements or point towards a product’s card.
We can observe that a card has the class called css-y5gcsw. Then within a card, we could see some data about products.
We are interested in a name, pricing, city, and image URLs of products so let’s see an HTML element of the data.
We can observe that we can have a name using css-1b6t4dn class, pricing with the css-1ksb19c class, a city using the css-1kdc32b class, and an image having a css-1c345mg class.
After identifying the HTML of this page, let’s make a script for getting data from a page.
After that, it’s time to make a file called scraper.py like a place for a Scraper to reside.
Let’s make a class called Scraper, which will get the responsibility of getting data from a website. Here, we make a property called driver, which will get filled with the Selenium Webdriver. A Webdriver is the class Selenium will utilize to create a session having a browser and connecting with a browser. Therefore, if a webdriver commands a browser to open any page, the page will get opened in a browser. To make a Webdriver object connected to the Firefox browser, we could call a static function Firefox() from a Webdriver class.
After that, let’s make a function called get_data() to find data from a website. For the objective here, we require to get an URL from a website. In case, we observe that website again, we could see an URL is :
Let’s create a driver command a browser to find the URL through calling the function driver.get("URL").
After that, Just make a counter for a page, which shows products and listing to place the data.
We would get data till page 10. For every page, we would make a driver command the browser for scrolling till the end of a page as the page would not load data in case, we didn’t scroll using it. When we checked the page, we found that a page has about 6500 pixels and we would scroll every 500 pixels. For every iteration, we would wait for 0.1 seconds thus we didn’t put any load together on the server.
After the repetition for scrolling, we would get a card’s element, iterator on all elements, find the name, pricing, image, and city data, and lastly put data to a datas variable.
Then, we find all data, we could go to next page through making a driver click to next page. In case, we check HTML of a page, we could find that a page button gets css-1ix4b60-unf-pagination-item class. And we could indicate which button is needed to click through using a counter variable.
And lastly, return data like a function’s return values.
For overall codes, just check this.
Now, let’s make a file called “main.py” for checking a class functionality. Just fill a file using this code.
If we run a file, we would open a Firefox browser, and a browser would automatically search as a driver instructed within our code. After that, we can observe the results from a terminal.
We could see that we found 700 product data from a shoe-searching page!!!
Then, we would try and present data in an additional format than printing in a terminal directly.
For more information, contact Actowiz Solutions now!
You can also reach for all your mobile app scraping and web scraping services requirements.
As June approaches, the anticipation for "Pride Month" grows, and a colorful transformation occurs across brand logos and websites worldwide. This blog shows how retailers celebrate pride month using LGBTQ+ inclusive campaigns and community support.
This Research Report discusses the 10 Biggest Apparel & Accessories Stores in 2023 in California As Per Locations. Contact Actowiz Solutions for any Apparel & Accessories Stores data scraping requirements.
This Research Report shows the 10 Biggest Florida Grocery Chains 2023, Depending on Locations. Contact Actowiz Solutions for all grocery chain data scraping requirements.