How to make html tables imported from the web into csv files from Python

Asked 2 years ago, Updated 2 years ago, 85 views

1 Answers

First of all, the error message is that the file could not be found.

I said files/editors.csv, but...See if you have the /files directory.

And when I tested it, the example above in Windows might have an encoding error.

Enrico Tröger

There is a string called, and in window-encoded cp949, Umroot (German letter with two dots) is an indescribable character.

Please test it with the example below. Simplified revisions to repeat statements or resource handling.

If you use with the text, it automatically organizes file resources.

The _csv.writer has a method called writerows that can work well on multiple lines at the same time.

import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
import codecs

html = urlopen("https://en.wikipedia.org/wiki/Comparison_of_text_editors")
bs0bj = BeautifulSoup(html, "html.parser")

table = bs0bj.findAll("table", {"class":"wikitable"})[0]

rows = table.findAll("tr")

with codecs.open("editors.csv", 'w', 'utf-8') as csvFile:
    writer = csv.writer(csvFile)
    csvRows = map(lambda row:(cell.get_text() for cell in row.findAll(['td', 'th'])), rows)
    writer.writerows(csvRows)


2022-09-21 19:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.