![](/img/trans.png)
[英]How to format list data and write to csv file in selenium python?
[英]How to write a text list to a defined csv file? Python selenium scraping
我是 Python 新手,所以如果這是一個簡單的問題,請原諒我。
我正在使用 Selenium 從 Linkedin 中抓取整個體驗部分。 以下是我的相關代碼:
from time import sleep
from selenium import webdriver
ChromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome('/Users/jones/Downloads/chromedriver')
driver.get('https://www.linkedin.com/in/pauljgarner/')
##writing 'Name' to excel
writer = csv.writer(open(parameters.file_name, 'w', encoding='utf8'))
writer.writerow(['Name'])
name = sel.xpath('normalize-space(//li[@class="inline t-24 t-black t-normal break-words"])').extract_first()
writer.writerow([name])
##scraping the entire work experience section:
experience = driver.find_elements_by_xpath('//section[@id = "experience-section"]/ul//li')
for item in experience:
print(item.text)
print("")
我從體驗部分得到的輸出是一個文本列表,如下所示:
Freelance Python Developer
Company Name
Depop
Dates Employed
Jun 2015 – Present
Employment Duration
4 yrs 11 mos
Location
London, United Kingdom
Python development using: Django, PostgreSQL, ElasticSearch, TensorFlow, Redis, gevent, Mongodb, Django REST Framework
我想將此輸出寫入我用來捕獲“名稱”的同一個 Excel 表中。
我正在尋找的 excel 格式如下所示:
Name Title CompanyName DatesEmployed EmploymentDuration Location Description
Paul Freel.. Depop Jun 2015 – P.. 4 yrs 11 mos London Python Dev..
問題是我不知道如何將我從體驗部分抓取的文本列表轉換為我之前使用特定元素(帶有“名稱”)定義的同一個 Excel 工作表。
嘗試這個:
from selenium import webdriver
ChromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome('/home/shubham/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)
print(item.text)
print("")
with open(parameters.file_name, 'w', encoding='utf8') as file:
writer = csv.writer(file)
writer.writerows(rows)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.