2017-03-22 3 views
0

J'essaye d'extraire quelques informations d'intrest à une excelsheet avec les en-têtes NOMS, FORMULA, EXACTMASS, MOLWEIGHT, CAS mais quand je cours ma boucle il ajoute chaque lettre/nombre ou byte (pas sûr si son bon terme) à une cellule. Je veux qu'il stocke toutes les informations qu'il affiche dans l'impression et stockez-le comme une chaîne dans chaque boîte pour chaque composé. Lorsque la boucle recommence pour le lien suivant, je veux qu'elle commence dans une nouvelle ligne. Je ne suis pas sûr d'où je vais mal.Stockage des données analysées avec Beatifulsoup4

import urllib 
import urllib.request 
from bs4 import BeautifulSoup 
import os 
import csv 

def make_soup(url): 
    thepage = urllib.request.urlopen(url) 
    soupdata = BeautifulSoup(thepage, "html.parser") 
    return soupdata 


compoundlist = [] 
soup = make_soup("http://www.genome.jp/dbget-bin/www_bget?ko00020") 
i = 1 
file = open("Compoundlist.csv", "w") 
for record in soup.findAll("nobr"): 
    compound = '' 
    if (record.text[0] == "C" and record.text[1] == '0') or (record.text[0] == "C" and record.text[1] == '1'): 
     compoundlist ="http://www.genome.jp/dbget-bin/www_bget?cpd:" + record.text[:6] + '\n' 
     file.write(compoundlist) 
     # print(compoundlist) 

file.close() 
compoundinfo = [] 
linklist =open('Compoundlist.csv') 

# 
# def CASnumber(soup): 
#  for tag in soup.findAll("div", {"style":"margin-left:3em"}): 
#   tag = tag.text 
#  return tag 


for items in linklist: 
    soupcomp = make_soup(items) 
    for data in soupcomp.findAll("div", {"style":"width:555px;overflow-x:auto;overflow-y:hidden"}): 
      for NAMES in soupcomp.findAll("div", {"style":"width:555px;overflow-x:auto;overflow-y:hidden"})[0]: 
       NAMES = NAMES.text 
    print(NAMES) 
    for data in soupcomp.findAll("div", {"style":"width:555px;overflow-x:auto;overflow-y:hidden"}): 
      for INFO in soupcomp.findAll("div", {"style":"width:555px;overflow-x:auto;overflow-y:hidden"})[0:3]: 
       FORMULA = INFO.text 
    print(FORMULA) 
    for data in soupcomp.findAll("div", {"style":"width:555px;overflow-x:auto;overflow-y:hidden"}): 
      for INFO in soupcomp.findAll("div", {"style":"width:555px;overflow-x:auto;overflow-y:hidden"})[0:4]: 
       EXACTMASS = INFO.text 
    print(EXACTMASS) 
    for data in soupcomp.findAll("div", {"style":"width:555px;overflow-x:auto;overflow-y:hidden"}): 
      for INFO in soupcomp.findAll("div", {"style":"width:555px;overflow-x:auto;overflow-y:hidden"})[0:5]: 
       MOLWEIGHT = INFO.text 
    print(MOLWEIGHT) 
    for data in soupcomp.findAll("div", {"style":"width:555px;overflow-x:auto;overflow-y:hidden"}): 
      for CAS in soupcomp.findAll("div", {"style":"margin-left:3em"}): 
       CAS = CAS.text 
    print(CAS) 
    with open("Compoundinfo.csv", 'a') as csv_file: 
      writer = csv.writer(csv_file) 
      writer.writerows([NAMES,FORMULA,EXACTMASS,MOLWEIGHT,CAS]) 

Répondre

0

Deux choses:

1) Mettre with open("Compoundinfo.csv", 'a') as csv_file: avant for items in linklist: - Il n'y a pas besoin de rouvrir le dossier à chaque boucle;

2) La méthode correcte pour votre boîtier est writer.writerow (vous avez writerows).

writerow prend des données unidimensionnelles et writerows prend des données bidimensionnelles en tant que paramètres.