Générer un fichier CSV à partir d'une liste
Je débute en Python et j'ai du mal avec l'export de données dans un fichier CSV.
J'utilise BeautifulSoup pour parser un document HTML, jusque là aucun problème.
J'ai donc une liste de toutes les occurences recherchées dans le doc HTML de la forme (id, categorie, nom, adresse, tel, url, id, categorie, nom, adresse, tel, url, id, categorie, nom, adresse, tel, url,....)
Comment je peux fractionner cette liste pour remplir mon fichier CSV correctement ? Ou comment m'y prendre autrement pour y arriver ?
Voilà ce que j'ai fait :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# coding: utf-8
import requests
import csv
from bs4 import BeautifulSoup
from unicodedata import normalize
url = "http://www.monsite.com/mapage.html"
r = requests.get(url)
soup = BeautifulSoup(r.content)
g_data = soup.find_all("li", {"class": "business"})
mylist = []
for item in g_data:
try:
mylist.append( item.find_all("span", {"class": "id"})[0].string)
except:
pass
try:
mylist.append(normalize('NFKD',item.find_all("div", {"class": "categorie"})[0].li.string).encode('ascii', 'ignore'))
except:
pass
try:
mylist.append(normalize('NFKD',item.find_all("a", {"class": "nom"})[0].span.text).encode('ascii', 'ignore'))
except:
pass
try:
mylist.append(normalize('NFKD',item.find_all("div", {"class": "adresse"})[0].p.text).encode('ascii', 'ignore'))
except:
pass
try:
mylist.append( item.find_all("ul", {"class": "telephone"})[-1].strong.text)
except:
pass
try:
mylist.append( item.find_all("a", {"class": "url"})[0].text)
except:
pass |