python:excel:在列中打印然后在下一行开始

时间:2017-10-20 19:53:24

标签: python-2.7 openpyxl

我有来自beautifulsoup的数据:

a
b
c
d
e
f

我希望以下列格式将它们放入excel中:

a b c d e f g 
h i j k l m n
o p q r s t u
etc...

当我在excel中打印它们时。

这是我目前的代码:

import openpyxl
from openpyxl import Workbook
import requests
from bs4 import BeautifulSoup
for i in range (1,2):
   url ="https:...."
   response=requests.get(url,verify=False)
   soup=BeautifulSoup(response.text)
   g_data=soup.find_all("td",{"class"})
   results=[]
   for item in g_data:
             data=(item.text)
             results.append(data)
             wb=Workbook()
             ws=wb.active
             for row, i in enumerate(results):
                      columns_cell='A'
                      ws[column_cell+str(row+2)]=str(i)
             wb.save("test.xlsx")

提前感谢您的帮助。

更新的代码:

for i in range (1,3):
url="https:.... .format(pagenum=i)
response=requests.get(url)
soup=BeautifulSoup(response.text)
g_data=soup.find_all("td","class")
row=[]
wb=Workbook()
ws=wb.active
for idx, item in enumerate(g_data):
    row.append(item.text)
    if not idx % 7:
        ws.append(row)
        row=[]
wb.save("test2.xlsx")

UPDATED RESULTS PICTURE:

最后这个有效:

for i in range (1,2)
url="https:... "
response=requests.get(url)
soup=BeautifulSoup(response.text)
g_data=soup.find_all("td",{"class"})
results=[]
for item in g_data:
    results.append(item.text)
df=pd.DataFrame(np.array(results).reshape(20,7),columns("abcdefg"))
writer=pd.ExcelWriter('test4.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()

这个问题是它覆盖了之前的结果。还有一些工作要做。但进步:)

1 个答案:

答案 0 :(得分:0)

听起来你想要的东西有点像这样:

row = []
for idx, item in enumerate(g_data):
     row.append(item.text)
     if not idx % 6: # 7th element:
         ws.append(row)
         row = []