Reputation: 122
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
Current excel (with attempted solution):
Upvotes: 0
Views: 318
Reputation: 4471
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.
Upvotes: 1