Python:将数据刮入Excel电子表格时无追溯

我是一个没有经验的编码器在Python中工作。 我写了一个脚本来自动化一个过程,在这个过程中,某些信息会从网页中被撕掉,然后被复制,粘贴到新的Excel电子表格中。 我已经编写并执行了代码,但是我指定接收数据的excel电子表格是完全空的。 最糟糕的是,没有回溯错误。 你能帮我在我的代码中find问题吗? 如果不提供回溯错误,通常如何解决自己的问题?

import xlsxwriter, urllib.request, string def main(): #gets the URL for the expert page open_sesame = urllib.request.urlopen('https://aries.case.com.pl/main_odczyt.php?strona=eksperci') #reads the expert page readpage = open_sesame.read() #opens up a new file in excel workbook = xlsxwriter.Workbook('expert_book.xlsx') #adds worksheet to file worksheet = workbook.add_worksheet() #initializing the variable used to move names and dates #in the excel spreadsheet boxcoA = "" boxcoB = "" #initializing expert attribute variables and lists expert_name = "" url_ticker = 0 name_ticker = 0 raw_list = [] url_list = [] name_list= [] date_list= [] #this loop goes through and finds all the lines #that contain the expert URL and name and saves them to raw_list:: #raw_list loop for i in readpage: i = str(i) if i.startswith('<tr><td align=left><a href='): raw_list += i #this loop goes through the lines in raw list and extracts #the name of the expert, saving it to a list:: #name_list loop for n in raw_list: name_snip = n.split('target=_blank>','</a></td><')[1] name_list += name_snip #this loop fills a list with the dates the profiles were last updated:: #date_list for p in raw_list: url_snipoff = p[28:] url_snip = url_snipoff.split('"')[0] url_list += url_snip expert_url = 'https://aries.case.com.pl/'+url_list[url_ticker] open_expert = urllib2.openurl(expert_url) read_expert = open_expert.read() for i in read_expert: if i.startswith('<p align=left><small>Last update:'): update = i.split('Last update:','</small>')[1] open_expert.close() date_list += update #now that we have a list of expert names and a list of profile update dates #we can work on populating the excel spreadsheet #this operation will iterate just as long as the list is long #meaning that it will populate the excel spreadsheet #with all of our names and dates that we wanted for z in raw_list: boxcoA = string('A',z) boxcoB = string('B',z) worksheet.write(boxcoA, name_list[z]) worksheet.write(boxcoB, date_list[z]) workbook.close() print('Operation Complete') main() 

缺less回溯只意味着你的代码没有任何例外。 这并不意味着你的代码在逻辑上是正确的。

我会通过添加打印语句或使用debugging器(如pdb或pudb)来查找逻辑错误。

我注意到你的代码的一个问题是,第一个循环似乎假设i是一条线,而它实际上是一个字符。 你可能会发现splitlines()更有用

如果没有回溯,那就没有错误。

您的抓取/parsing代码很可能出了问题,并且您的raw_list或其他数组未填充。

尝试在最后一个循环中打印出应该写入工作表的数据,看是否有数据要写入。

如果你不写数据到工作表,那么它将是空的。