0

我正在尝试将列表拆分为单独的单元格。

但是,没有分隔列表的逗号分隔符,只有换行符。

我已经阅读了其他一些具有相同属性错误的帖子,但仍然没有弄清楚我哪里出错了。

我的相关代码:

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'))

该列表来自抓取“经验”:

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

我的代码的最后四行似乎应该可以解决问题,但是我收到了属性错误。我哪里错了?非常感谢您的帮助

更新:理想的excel输出: 在此处输入图像描述

当前的excel(尝试解决方案): 在此处输入图像描述

4

1 回答 1

1

您会收到此错误,因为rows它始终是一个包含单个元素的列表,这也是一个列表。splitstr实例上的方法,而不是列表。您可以看到情况就是这样,因为您只append需要第一个元素:

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

您可以通过构造rows字符串列表来简化代码:

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

现在,您的 CSV 编写器将按您的预期工作。

于 2020-04-09T08:46:21.257 回答