DEV Community

DailyFlutter.Monster
DailyFlutter.Monster

Posted on • Originally published at dailyflutter.monster

Flutter Web Scraper Level 2: Etsy Product Finder Part 2

Intro

I am showing how to build a etsty product searcher utilizing webscraping in flutter. In the previous part (link here) I showed how to set the ui, and we ended with this

Alt Text

Our goal for this part is to add the url build to make an etsy search and the webscraping of the search results. In the end we will have an app that looks like this:

Alt Text

Tutorial Portion

'Please enter search' text

The first thing we will add is a loading indicator for when we are not searching. For this

in home_screen.dart we add:

Webscraping Time

In results_list.dart we are going to add the changes to scrape the products from Esty's page. I will just add the code for the new result_list.dart file and explain some of the most important lines bellow:

First, in lines 11-12, we have a boolean to check if the webscraping has loaded, and a list of result items that we will be adding to the list.

Lines 14 is the the main Estys url and line 15 is the particular page we will be searching.

We have an initState function that calls the async getData() function.

getData() is the function where we will be scraping the page. First we initialize a new WebScraper variable that has the estyUrl. Then in an if statement we check if the webpage we chose gets loaded. For the webpage in line 27, you can see that we have to construct the URL, by adding the search onto our pageExtenstion. The '.trim().replaceAll(' ', '%20')' portion is to make sure that if we have spaces in our search we are trimming them and changing any space between words to '%20' which is the standard that Etsy uses.

Then we create three List> to get the images, descriptions, prices, and URLs. The tags used in the getElement() call were found using the web inspector in chrome on the Etsy page

Alt Text

We the iterate through one of the lists (in this case I chose images) and custruct our ResultItem then add it to the list.

Once all this is done we call setState() and set loaded to true so that the results list will be show.

And that's it!

Now if you run it you should get the end product

Alt Text

Conclusion

I hope this projust tought you how to search and scrape html pages to display them in a list to users. This is a great idea if you want to create a comparison searcher, you could then scrape two websites and display all the result.

In a future post I will also be showing how to scrape Javascript website, something that is a bit more tricky and requires more setting up, so stay tuned to for that one if you are interested.

Top comments (0)