2017-07-03 179 views
1

我試圖將生成的列表導出到csv文件,其中網站表中的每一行對應於文件中的新行,並且每個值都位於單個單元格中,例如:將python列表值寫入csv文件

NAME.....ICO DATE....ICO PRICE....CURR. PRICE....24 HR ROI Stratis.....06/20/16.......$0.007...........$7.480................+38.80%

電流輸出看起來是這樣的:

['Patientory\n05/31/17\n$0.104\n$0.274\n+46.11%\n+25.54%\nN/A']

import csv 
from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.ui import WebDriverWait as wait 

csvrows = [] 

def get_css_sel(selector): 
    posts = browser.find_elements_by_css_selector(selector) 
    for post in posts: 
     print(post.text) 
     csvrows.append([post.text]) 

browser = webdriver.Chrome(executable_path=r'C:\Scrapers\chromedriver.exe') 
browser.get("https://icostats.com") 
wait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#app > div > div.container-0-16 > div.table-0-20 > div.tbody-0-21 > div:nth-child(2) > div:nth-child(8)"))) 

get_css_sel("#app > div > div.container-0-16 > div.table-0-20 > div.tableheader-0-50")    #fetch header of table 
get_css_sel("#app > div > div.container-0-16 > div.table-0-20 > div.tbody-0-21 > div")    #fetch rows of table 

def create_csv(thelist): 
    with open('ICO.csv', 'w') as myfile: 
     for i in thelist: 
      wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) 
      wr.writerow([i]) 

create_csv(csvrows) 

回答

2

get_css_sel(),每個post.text包含換行符\n分開行文字 - 與您的examp輸出。所以追加[post.text]附加一個列表與單個項目的整個行。修改成:

csvrows.append(post.text.split('\n')) # remove the extra list brackets 
             # since split returns a list. 

例:

>>> y = 'Patientory\n05/31/17\n$0.104\n$0.274\n+46.11%\n+25.54%\nN/A' 
>>> y.split('\n') 
['Patientory', '05/31/17', '$0.104', '$0.274', '+46.11%', '+25.54%', 'N/A'] 

此外,在你的寫作圈,你不應該重新創建csv.writer的每一行,遍歷thelist之前就去做一次。

由於您擁有csvrows中的所有行,因此您可以直接使用csvwriter.writerows

def create_csv(thelist): 
    with open('ICO.csv', 'w') as myfile: 
     wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) 
     wr.writerows(thelist) 
+0

這就是它!另外,如何刪除引號?我無法調用remove():AttributeError:'NoneType'對象沒有屬性'remove' – tklein

+0

你試圖刪除哪些引號?在CSV中,您已將'quoting = csv.QUOTE_ALL'。刪除,如果你不想要不必要的引號。此外,[默認的'dialect'是'excel'](https://docs.python.org/3/library/csv.html#csv.writer)通常就足夠了。 – aneroid

+0

如果你得到空行並且沒有'post.text'的文本,那麼在'csvrows.append ...'之前放置'if post.text:'。 – aneroid

1

試試這個代碼:

import csv 
from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.ui import WebDriverWait as wait 

csvrows = [] 
def get_css_sel(selector): 
    posts = browser.find_elements_by_css_selector(selector) 
    for post in posts: 
     print(post.text) 
     csvrows.append(post.text) 

browser = webdriver.Chrome(executable_path=r'//Users/Pranavtadepalli/Downloads/chromedriver') 
browser.get("https://icostats.com") 
wait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#app > div > div.container-0-16 > div.table-0-20 > div.tbody-0-21 > div:nth-child(2) > div:nth-child(8)"))) 

get_css_sel("#app > div > div.container-0-16 > div.table-0-20 > div.tableheader-0-50")    #fetch header of table 
get_css_sel("#app > div > div.container-0-16 > div.table-0-20 > div.tbody-0-21 > div")    #fetch rows of table 
new=[",".join(elem.split("\n")) for elem in csvrows] 
newfile=open("csvfile.csv",'r') 
newfile1=open("csvfile.csv",'w') 
newstuff=newfile.read() 
for elem in new: 
    newfile1.write(elem+'\n') 
newfile1.close() 
newfile.close()