L
L
love_parsing2020-06-16 21:48:47
Python
love_parsing, 2020-06-16 21:48:47

How beautiful to add data to csv?

Good afternoon everyone. There is a parser that parses the site https://3dnews.ru/news
and enters the data into a csv table separated by commas.
How can you put data into the table not separated by commas, but the name in the first column, and the link in the second column?
For the next article: The title is in the first column, and the link is in the second column (in general, not separated by commas, but each in its own column)
Code:

import requests
from bs4 import BeautifulSoup
import csv


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


def csv_read(data):
    with open("data.csv", 'a') as file:
        writer = csv.writer(file)
        writer.writerow((data['head'], data['link']))

def get_link(html):
    soup = BeautifulSoup(html, 'lxml')
    head = soup.find('div', id='section-content').find_all('a', class_="entry-header")
    for i in head:
        link = 'https://3dnews.ru' + i.get('href')
        heads= i.find('h1').string
        data = {'head': heads,
                 'link': link}
        csv_read(data)


data = get_link(get_html('https://3dnews.ru/news'))

Conclusion:

5ee913448bf38554103248.png

How to draw such a conclusion?

5ee913c6601e3800467939.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-06-16
@love_parsing

CSV - Comma -Separated Values
​​This is a format in which data is separated by commas rather than columns. This is already a question for the program into which you import the data. It should itself be able to split the data into columns. Excel should split by default, Google's analogue docs.google.com asks for which character to split the data.
In general, in csv everything must be separated by commas or other signs, splitting into columns is the task of the program that reads this file.

V
Vladimir Kuts, 2020-06-16
@fox_12

csv.DictWriter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question