📄

A Python Engineer Tries Scraping with Ruby

This article was automatically translated from theJapanese original by AI. It may contain translation errors.

This post was published over 2 years ago. Its content may be outdated.

Introduction

I usually write code in Python, but recently I’ve started getting interested in Ruby too. I was curious how much the same code differs from one language to another, so I did the same scraping using Selenium in both Ruby and Python. This is output for learning purposes, so I’d appreciate it if you’d point out any mistakes.

Scraping with Ruby

1. Setting up the package

gem install selenium-webdriver

2. Code

require 'selenium-webdriver'

options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
driver = Selenium::WebDriver.for(:chrome, options: options)

driver.navigate.to 'https://qiita.com'

puts driver.title

elements = driver.find_elements(tag_name: 'a')
elements.each do |element|
  puts element.attribute('href')
end

driver.quit

3. Running it

ruby main.rb
>> Qiita
>> https://qiita.com/
>> ....

Scraping with Python

1. Setting up the package

pip install selenium

2. Code

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)

driver.get("https://qiita.com")

print(driver.title)

elements = driver.find_elements(By.TAG_NAME, "a")
for element in elements:
    print(element.get_attribute("href"))

driver.quit()

3. Running it

python main.py
>> Qiita
>> https://qiita.com/
>> ....

Comparing the code

Screenshot

Summary

I tried doing the same scraping in Python and Ruby. There are differences in how the code is written, but I felt that the basic parts are similar. My impression is that Ruby often ends up shorter than Python, but I found both to be easy-to-use languages. Personally I found Python easier to write, but I’d like to become able to make good use of Ruby too.

Recent Articles

Network(beta)

Drag to move / Ctrl+wheel to zoom