2016-12-24 60 views
0

我試圖從使用不同數字的列的url中的表中繪製數據。我創建了一個數字列表,然後在URL的末尾輸入每個數字,從每個唯一鏈接中提取數據並將數據輸入到列表中。然後我把這個列表寫到一個excel文件中,但是當我這樣做時,當我需要每行唯一鏈接需要一行時,數據被寫入一行。使用python在excel中寫入多行

import xlrd 
from bs4 import BeautifulSoup 
import requests 
import csv 
import urllib 

sheet = xlrd.open_workbook('/Users/stevenschwab/Downloads/2016 Preliminary Assessments.xlsx') 
sh = sheet.sheet_by_index(0) 
numbers = sh.col_values(0) 
data = [] 

for i in range(3,len(numbers)): 
    data.append(int(numbers[i])) 

for j in range(0,5): 
    print(data[j]) 


for key in data: 
    url = 'http://algonquin.northwoodsoft.com/display/PropertySearch.asp?cmd=DisplayDetails&ky= + key +' 
    response = requests.get(url) 
    html = response.content 
    soup = BeautifulSoup(html,"html5lib") 
    table = soup.find('center', attrs={'xmlns:dt':'urn:schemas-microsoft-com:datatypes'}) 
    rows = [] 

    for row in table.findAll('tr')[1:]: 
     cells = [] 

     for cell in row.findAll('td'): 
      cells.append(cell.text) 
    rows.append(cells) 

outfile = open('./property.csv', 'w') 
writer = csv.writer(outfile) 
writer.writerows([rows]) 
+0

您沒有正確連接URL中的*鍵*。 – Parfait

回答

0

在循環的每次迭代中,您正在清除rows變量。將您的rows = []移到循環之外。