-1

I'm trying to fetch and export the data in the front page of https://icostats.com/ to a .csv file. This is my code so far:

import csv
from selenium import webdriver

def get_elements_by_xpath(driver, xpath):
    return [entry.text for entry in driver.find_elements_by_xpath(xpath)]

url = ("https://icostats.com")
driver = webdriver.Firefox(executable_path=r'C:\Users\alph1\Scrapers\geckodriver.exe')
driver.get(url)

search_entries = [
    ("NAME", "//div[@class='tdName-0-73']"),
    ("DATE", "//div[@class='tdDate-0-74']"),
    ("CUR PRICE", "//div[@class='tdPrice-0-72'][1]"),
    ("ICO PRICE", "//div[@class='tdPrice-0-72'][0]"),
    ("24H ROI", "//div[@class='tdPrimary-0-75']")]

with open('textfile.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)

    # Write header
    csv_output.writerow([name for name, xpath in search_entries])
    entries = []

    for name, xpath in search_entries:
        entries.append(get_elements_by_xpath(driver, xpath))

    csv_output.writerows(zip(*entries))

get_elements_by_xpath()

And here is the exception I'm getting.

File "C:/Users/alph1/PycharmProjects/PyQtPS/ICO2CSV.py", line 28, in csv_output.writerow([name for name, xpath in search_entries]) TypeError: a bytes-like object is required, not 'str'

I have a feeling I shouldn't be calling the method like this in the end, but don't know how else I'd do it.

tklein
  • 77
  • 2
  • 8
  • Take a look at https://stackoverflow.com/questions/37289951/python-write-to-csv-line-by-line and https://stackoverflow.com/questions/6916542/writing-list-of-strings-to-excel-csv-file-in-python, might help. – Deem Jun 30 '17 at 02:01
  • Thanks, but not sure of how it would apply in my case. – tklein Jun 30 '17 at 18:29

1 Answers1

0

I believe this is due to the fact that you have opened the file as a binary (open('textfile.csv', 'wb'). This means that all data from it will be read in binary, as bytes.

To remedy this, simply do open('textfile.csv', 'w') instead.

Deem
  • 7,007
  • 2
  • 19
  • 23