{"id":773,"date":"2018-12-15T06:00:57","date_gmt":"2018-12-15T06:00:57","guid":{"rendered":"http:\/\/buklijas.info\/blog\/?p=773"},"modified":"2018-12-14T21:12:19","modified_gmt":"2018-12-14T21:12:19","slug":"do-not-use-selenium-for-web-scraping","status":"publish","type":"post","link":"http:\/\/buklijas.info\/blog\/2018\/12\/15\/do-not-use-selenium-for-web-scraping\/","title":{"rendered":"Do not use Selenium for web scraping"},"content":{"rendered":"
Published on<\/strong>: 15.12.2018<\/p>\n Disclaimer: I have noticed that Selenium<\/a> has become quite popular<\/a> for scraping<\/a> data from web pages.<\/p>\n Yes, you can use Selenium for web scraping, but it is not a good idea<\/strong>.<\/p>\n Also personally, I think that articles that teach how to use Selenium for web scraping are giving a bad example of what tool to use for web scraping<\/strong>.<\/p>\n First,Selenium is not a web scraping tool<\/strong>.<\/p>\n It is “for automating web applications for testing purposes”<\/strong> and this statement is from the homepage of Selenium.<\/p>\n Second, in Python, there is a better tool Scrapy<\/a> open-source web-crawling framework<\/strong>.<\/p>\n The intelligent reader will ask: “What is a benefit in using Scrapy over Python?<\/strong>”<\/p>\n You get speed and a lot of speed<\/strong> (not Amphetamine :-)), speed in development and speed in web scraping time.<\/p>\n There are tips on how to make Selenium web<\/a> scraping faster<\/a>, and if you use Scrapy then you do not have those kinds of problems and you are faster<\/strong>.<\/p>\n Just because these articles exist is proof (at least for me) that people are using the wrong tool for the job<\/strong>, an example of “When your only tool is a hammer, everything looks like a nail<\/strong>“.<\/p>\n I personally only use Selenium for web page testing<\/strong>.<\/p>\n I would try to use it for automating web applications<\/strong> (if there are no other options), but I never had that use case so far.<\/p>\n The only exception that I could see for using Selenium as web scraping tool is if a website that you are scraping is using JavaScript to get\/display data that you need to scrape<\/strong>.<\/p>\n Scrapy does have the solution for JavaScript<\/a> with Splash<\/a>, but I have never used it, so far I always found some workaround.<\/p>\n As you can guess, my advice is to use Scrapy<\/a>.<\/p>\n I choose Scrapy because I spend less time<\/strong> developing web scraping programs (web spiders) and execution time is fast.<\/p>\n I have found Scrapy to be faster in development time because of a Scrapy shell<\/a> and cache<\/a>.<\/p>\n In execution, it is fast because multiple requests can be done simultaneously<\/strong>, this means that data delivery will not be in the same order as requested<\/a>, just that you are not confused when debugging.<\/p>\n I have used this combination in the past before I decided to invest time in learning Scrapy<\/strong>.<\/p>\n Do not make the same mistake as I did<\/strong>, development time and execution time is much faster with Scrapy, than with any other tool that I have found so far.<\/p>\n This is not rant about using Selenium for web scraping, for not production system and learning\/hobby it is fine<\/strong>.<\/p>\n I get it, Selenium is easy to start and you can see what is happing in real time on your screen<\/strong>, that is a huge benefit for people starting to do\/learn web scraping and it is important to have this kind of early moral bosts<\/strong> when you are learning something new.<\/p>\n But I do think that all these article and tutorial using Selenium for web scraping should have a disclaimer not to use Selenium in real life (if you need to scrape 100K pages in a day, it is not possible to do it in single Selenium instance)<\/strong>.<\/p>\n To start with Scrapy it is harder, you have to write XPath selectors and look at source code of HTML page to debug is not fun, but if you want to have fast web scraping that is the price<\/strong>.<\/p>\n After you learn Scrapy you will be faster than with Selenium<\/strong> (Selenium just have a lower-angle learning curve), I personally needed a few days to get the basics.<\/p>\n","protected":false},"excerpt":{"rendered":" Published on: 15.12.2018 Disclaimer: This is primarily written from Python programming language ecosystem point of view. I have noticed that Selenium has become quite popular for scraping data from web pages. Yes, you can use Selenium for web scraping, but it is not a good idea. Also personally, I think that articles that teach how […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"Do not use Selenium for web scraping #selenium #scrapy #Python \"webscraping","jetpack_is_tweetstorm":false},"categories":[27],"tags":[4,42,17,38],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"yoast_head":"\n
\nThis is primarily written from Python programming language ecosystem point of view<\/strong>.<\/p>\nWhy you should not use Selenium for web scraping<\/h3>\n
For what should you use Selenium<\/h3>\n
Exception on when you can use Selenium<\/h3>\n
What to use instead of Selenium for web scraping<\/h3>\n
What about Beautiful Soup<\/a> + Requests<\/a><\/h3>\n
Last words<\/h3>\n
Conclusion<\/h3>\n