{"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:
\nThis is primarily written from Python programming language ecosystem point of view<\/strong>.<\/p>\n

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

Why you should not use Selenium for web scraping<\/h3>\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

For what should you use Selenium<\/h3>\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

Exception on when you can use Selenium<\/h3>\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

What to use instead of Selenium for web scraping<\/h3>\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

What about Beautiful Soup<\/a> + Requests<\/a><\/h3>\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

Last words<\/h3>\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

Conclusion<\/h3>\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":"\nDo not use Selenium for web scraping - Sasa Buklijas<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/buklijas.info\/blog\/2018\/12\/15\/do-not-use-selenium-for-web-scraping\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Do not use Selenium for web scraping - Sasa Buklijas\" \/>\n<meta property=\"og:description\" content=\"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 […]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/buklijas.info\/blog\/2018\/12\/15\/do-not-use-selenium-for-web-scraping\/\" \/>\n<meta property=\"og:site_name\" content=\"Sasa Buklijas\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-15T06:00:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-12-14T21:12:19+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\">\n\t<meta name=\"twitter:data1\" content=\"Sasa Buklijas\">\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data2\" content=\"3 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/buklijas.info\/blog\/#website\",\"url\":\"http:\/\/buklijas.info\/blog\/\",\"name\":\"Sasa Buklijas\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"http:\/\/buklijas.info\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/buklijas.info\/blog\/2018\/12\/15\/do-not-use-selenium-for-web-scraping\/#webpage\",\"url\":\"http:\/\/buklijas.info\/blog\/2018\/12\/15\/do-not-use-selenium-for-web-scraping\/\",\"name\":\"Do not use Selenium for web scraping - Sasa Buklijas\",\"isPartOf\":{\"@id\":\"http:\/\/buklijas.info\/blog\/#website\"},\"datePublished\":\"2018-12-15T06:00:57+00:00\",\"dateModified\":\"2018-12-14T21:12:19+00:00\",\"author\":{\"@id\":\"http:\/\/buklijas.info\/blog\/#\/schema\/person\/780025d597f1c5df3cc156eaffc8c561\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/buklijas.info\/blog\/2018\/12\/15\/do-not-use-selenium-for-web-scraping\/\"]}]},{\"@type\":\"Person\",\"@id\":\"http:\/\/buklijas.info\/blog\/#\/schema\/person\/780025d597f1c5df3cc156eaffc8c561\",\"name\":\"Sasa Buklijas\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/buklijas.info\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/0.gravatar.com\/avatar\/9f6f7de5a4882517ca0e4a8ebd607925?s=96&d=mm&r=g\",\"caption\":\"Sasa Buklijas\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5YHGV-ct","_links":{"self":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts\/773"}],"collection":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/comments?post=773"}],"version-history":[{"count":13,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts\/773\/revisions"}],"predecessor-version":[{"id":787,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts\/773\/revisions\/787"}],"wp:attachment":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/media?parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/categories?post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/tags?post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}