0

I am trying to automate a search on Google because I have more than 1 thousand lines. I can read and automate my search from the CSV, but I cannot add the array to the file. Maybe I'm missing something?

For the test, the CSV file is made up of 1 column with no header and 3 rows.

Here is my code:

require 'watir'
require 'nokogiri'
require 'csv'

browser = Watir::Browser.new(:chrome)
browser.goto("http://www.google.com")
CSV.open('C:\Users\Market\Documents\Emailhunter_scraper\test-email.csv').map do |terms|
browser.text_field(title: "Rechercher").set terms
browser.send_keys :return

sleep(rand(10))
doc = Nokogiri::HTML.parse(browser.html)
doc.css("div.f kv _SWb").each do |item|
  name = item.css('a').text
  link = item.css('a')[:href]
  csv << [name, link]
end
sleep(rand(10))
end
sleep(rand(10))
3
  • I'm guessing the issue is that you haven't opened up the CSV file for writing. Try this: CSV.open('path/to/file/csv', 'wb') Commented Sep 15, 2017 at 13:11
  • Thanks for your solutions, indeed it does write in my csv. Only problem is: it writes nothing and erases everything written... What am I doing wrong? My csv file has 2 columns (entities,url) and 3 rows (a,b,c) I know how I can tell Ruby to look for each row but not how I can tell Ruby to write in column url the link parsed corresponding to the entity read. I don't know if I'm clear enough so don't hesitate to hit me back ;) Commented Sep 18, 2017 at 15:55
  • Any update on my problem? Commented Sep 20, 2017 at 9:12

1 Answer 1

1

As shown in the documentation for CSV.open, the file mode defaults to "rb".

This means the file is being opened as read-only. Instead, you need to use:

CSV.open('path/to/file/csv', 'wb')

The full documentation for different modes can be seen here. They are:

"r"  Read-only, starts at beginning of file  (default mode).

"r+" Read-write, starts at beginning of file.

"w"  Write-only, truncates existing file
     to zero length or creates a new file for writing.

"w+" Read-write, truncates existing file to zero length
     or creates a new file for reading and writing.

"a"  Write-only, each write call appends data at end of file.
     Creates a new file for writing if file does not exist.

"a+" Read-write, each write call appends data at end of file.
     Creates a new file for reading and writing if file does
     not exist.

"b"  Binary file mode
     Suppresses EOL <-> CRLF conversion on Windows. And
     sets external encoding to ASCII-8BIT unless explicitly
     specified.

"t"  Text file mode
Sign up to request clarification or add additional context in comments.

1 Comment

OMG i'm so stupid! you answered me ages ago and I just understood... Thank you so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.