我正在尝试使用标题NAMES,FORMULA,EXACTMASS,MOLWEIGHT,CAS将exrestsheet的一些信息提取到excelsheet但是当我运行我的循环时,它会添加每个字母/数字或字节(不确定它是否正确的术语)一个细胞。我希望它存储它在打印中显示的全部信息,并将其作为字符串存储在每个化合物的每个框中。当循环再次开始下一个链接时,我希望它在一个新行开始。我不知道我哪里出错了。
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])
答案 0 :(得分:0)
两件事:
1)将with open("Compoundinfo.csv", 'a') as csv_file:
放在for items in linklist:
之前 - 无需在每个循环中重新打开文件;
2)您的案例的正确方法是writer.writerow
(您有writerows
)。
writerow
获取一维数据,writerows
获取二维数据作为参数。