简体   繁体   中英

How to write a text list to a defined csv file? Python selenium scraping

I am new to Python, so please forgive me if this is a simple issue.

I am web scraping an entire experience section from Linkedin using Selenium. Below is my relevant code:

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("")

The output I get from the experience section is a text list that looks like the below:

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

I want to write this output into the same excel sheet I used to capture the 'Name'.

The excel format I'm looking for would look like:

Name  Title   CompanyName  DatesEmployed  EmploymentDuration  Location  Description
Paul  Freel.. Depop        Jun 2015 – P.. 4 yrs 11 mos        London    Python Dev..

The issue is that I do not know how to convert the text list I scraped from the experience section into the same Excel sheet that I defined earlier with a specific element (with 'Names').

Try this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM