簡體   English   中英

使用python在Excel中編寫多行

[英]Writing multiple rows in excel using python

我正在嘗試使用一列不同的數字從URL的表格中提取數據。 我創建了一個數字列表,然后在url的末尾輸入每個數字,從每個唯一鏈接中提取數據,然后將該數據輸入到列表中。 然后,我將列表寫入一個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])

您將在循環的每次迭代中清除rows變量。 將您的rows = []移出循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM