N
N
Nikita2020-02-06 14:11:20
Python
Nikita, 2020-02-06 14:11:20

How to unload data in excel from the parser by columns?

Wrote a simple parser that collects plugin data from Wordpress. Everything works stably, but when exporting this data to a csv file, each entry about the plugin is written in one cell separated by commas, when everything should be signed into lines. That is, the name in one cell, the url to the right of it, the number to the right of the url.
I am using the standard csv library.
Tell me how can this be fixed?

import requests
from bs4 import BeautifulSoup 
import csv

def get_html(url):
   r = requests.get(url)
   return r.text

def refined(s):
    r = s.split(' ')[0]
    result = r.replace(',', '')
    return result

def write_csv(data):
    with open('plugins.csv','a') as f:
        writer = csv.writer(f)
        writer.writerow( (data['name'], data['url'], data['reviews']) )

def get_data(html):
    soup = BeautifulSoup(html, 'lxml')
    popular = soup.find_all('section') [1]
    plugins = popular.find_all('article')
    
    for plugin in plugins: 
        name = plugin.find('h2').text
        url = plugin.find('h2').find('a').get('href')
        r = plugin.find('span', class_= 'rating-count').find('a').text
        rating = refined(r) 

        data = { 'name': name,
        'url': url,
        'reviews': rating }
        write_csv(data)

def main():
    url = 'https://wordpress.org/plugins/'
    get_data(get_html(url))

if __name__ == '__main__':
    main()

5e3bf44db10b2588291053.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Radjah, 2020-02-06
@uptownnegative

Separate fields with ";" or use export to Excel with manual delimiter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question