Search code examples
pythonpython-3.xpandasselenium-webdriverexport-to-csv

How to get list into ONE cell in csv file python ( in my case list of authors of a book ), all authors in one cell


I am trying to get names of authors of a book from a books website. I get the names in one column but multiple rows. I want all names in one cell of csv. Following is my full code

from selenium import webdriver    
import pandas as pd    
driver = webdriver.Chrome()    
site = 'https://www.goodreads.com/book/show/50148349-manto-and-chughtai?from_search=true&from_srp=true&qid=ZARMElvyyt&rank=3'

driver.get(site)    
authors = [ ]    
names = driver.find_elements_by_xpath('//div[@class="authorName__container"]')

for name in names:
    authors.append(name.find_element_by_xpath('.//a[@class="authorName"]').text)

df = pd.DataFrame({'Author Names': authors})    
df.to_csv("Authors_list.csv", index=False)
print(df)

Here is my output, I am getting, I want all these four names in one cell

enter image description here


Solution

  • You could try this.

    authors = ','.join(df['authors'].to_list())
    with open('mycsv.csv', 'w', newline='') as myfile:
        myfile.write(authors)