2017-10-20 130 views
0

我在的形式從beautifulsoup數據:蟒蛇:Excel中:打印在列然後開始下一行

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() 

與這一個問題是,它的覆蓋以前的結果。還有一些工作要做。但進展:)

回答

0

聽起來像是你想要的東西有點像這樣:

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

編輯你的問題,以顯示在代碼中,你所看到的在細胞有什麼變化。 –