0

I'm attempting to split a list into separate cells.

However, there is no comma delimiter that separates the list, only a line break.

I've read a few other posts with the same attribute error, and still haven't figured out where I am going wrong.

My relevant code:

from selenium import webdriver

ChromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome('/Users/jones/Downloads/chromedriver')

driver.get('https://www.linkedin.com/in/pauljgarner/')

rows = []

name = sel.xpath('normalize-space(//li[@class="inline t-24 t-black t-normal break-words"])').extract_first()
experience = driver.find_elements_by_xpath('//section[@id = "experience-section"]/ul//li')

rows.append([name])
for item in experience:
    rows[0].append(item.text)
    print(item.text)
    print("")

with open(parameters.file, 'w', encoding='utf8') as file:
    writer = csv.writer(file)
    for row in rows:
        writer.writerow(row.split('\n'))

The list is from scraping 'experience':

Freelance Python Developer
Company Name
Depop
Dates Employed
Jun 2015 – Present
Employment Duration
4 yrs 11 mos

The last four lines of my code seem like they should do the trick, but instead I receive the attribute error. Where am I going wrong? Your help is much appreciated

UPDATE: Ideal excel output: enter image description here

Current excel (with attempted solution): enter image description here

1
  • split() is a method related to str object, and your row variable is of type list, hence you are facing this error. Commented Apr 9, 2020 at 5:27

1 Answer 1

1

You get this error because rows is always going to be a list with a single element, which is also a list. split is a method on str instances, not lists. You can see that this is the case because you only ever append to the first element:

for item in experience:
    rows[0].append(item.text)

You may simplify your code by constructing rows as a list of strings:

rows = [name]
for item in experience:
    rows.append(item.text)

Now, your CSV writer will work as you expect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.