每当我在CMD中运行程序时,都会收到缩进错误。对我来说,整个程序中的缩进看起来都很完美,所以我完全不知道为什么会收到错误。
CMD错误:
scraper9.py", line 50
browser.get(url2)
^
IndentationError: unexpected unindent
我已完全删除所有缩进,并逐行缩进以达到当前迭代,但我仍然出错。
导入操作系统 导入系统 导入csv 从bs4导入BeautifulSoup 导入urllib2 导入xlsxwriter 从硒导入webdriver
reload(sys)
sys.setdefaultencoding("utf8")
key_stats_on_main = ["Market Cap", "PE Ratio (TTM)"]
key_stats_on_stat = ["Enterprise Value", "Trailing P/E"]
stocks_arr =[]
pfolio_file = open("tickers.csv", "r")
for line in pfolio_file:
indv_stock_arr = line.strip().split(",")
stocks_arr.append(indv_stock_arr)
print(stocks_arr)
browser = webdriver.PhantomJS()
stock_info_arr = []
for stock in stocks_arr:
stock_info = []
ticker = stock[0]
stock_info.append(ticker)
url="https://finance.yahoo.com/quote/{0}?p={0}".format(ticker)
url2="https://finance.yahoo.com/quote/{0}/key-statistics?p={0}".format(ticker)
browser.get(url)
innerHTML = browser.execute_script("return document.body.innerHTML")
soup = BeautifulSoup(innerHTML, "html.parser")
for stat in key_stats_on_main:
page_stat1 = soup.find(text = stat)
try:
page_row1 = page_stat1.find_parent("tr")
try:
page_statnum1 = page_row1.find_all("span")[1].contents[1]
except:
page_statnum1 = page_row1.find_all("td")[1].contents[0]
except:
print("Invalid parent for this element")
page_statnum1 = "N/A"
stock_info.append(page_statnum1)
browser.get(url2)
innerHTML2 = browser.execute_script("return document.body.innerHTML2")
soup2 = BeautifulSoup(innerHTML2, "html.parser")
for stat in key_stats_on_stat:
page_stat2 = soup2.find(text=stat)
try:
page_row2 = page_stat2.find_parent("tr")
try:
page_statnum2 = page_row2.find_all("span")[1].contents[0]
except:
page_statnum2 = page_row2.find_all("td")[1].content[0]
except:
print("Invalid pareent for this element")
page_statnum2 = "N/A"
stock_info.append(page_statnum2)
stock_info_arr.append(stock_info)
print(stock_info_arr)
key_stats_on_main.extend(key_stats_on_stat)
workbook = xlsxwriter.Workbook("Stocks01.xlsx")
worksheet = workbook.add_worksheet()
row = 0
col = 2
for stat in key_stats_on_main:
worksheet.write(row, col, stat)
col +=1
row = 1
col = 0
for our_stock in stock_info_arr:
col = 0
for info_bit in our_stock:
worksheet.write(row, col, info_bit)
col += 1
row += 1
workbook.close()
print("Script completed")
我希望代码执行时不会出现Ineation错误。
它出错了。我很迷路。
答案 0 :(得分:9)
您的try:
缺少except:
或finally:
。
for stat in key_stats_on_main:
page_stat1 = soup.find(text = stat)
try: # <--------------- this one here
page_row1 = page_stat1.find_parent("tr")
try:
page_statnum1 = page_row1.find_all("span")[1].contents[1]
except:
page_statnum1 = page_row1.find_all("td")[1].contents[0]
except:
print("Invalid parent for this element")
page_statnum1 = "N/A"
stock_info.append(page_statnum1)
# <---------------- needs something here
browser.get(url2)
您可能是说第二个except:
位于第一个try:
的缩进级别:
for stat in key_stats_on_main:
page_stat1 = soup.find(text = stat)
try: # <--------------- this one here
page_row1 = page_stat1.find_parent("tr")
try:
page_statnum1 = page_row1.find_all("span")[1].contents[1]
except:
page_statnum1 = page_row1.find_all("td")[1].contents[0]
except:
print("Invalid parent for this element")
page_statnum1 = "N/A"
stock_info.append(page_statnum1)
browser.get(url2)
尝试一下!
顺便说一句,您应该减小try:
子句中代码的大小,并仅捕获要处理的异常。对于您而言,AttributeError
(如果.contents
失败)将被第一个except:
捕获。更好:
try:
found = page_row1.find_all("span")
index = 1
except XError: # should be the one .find_all() can raise
found = page_row1.find_all("td")
index = 0
page_statnum1 = found[1].contents[index]
外部try
/ except
的情况与此类似。
这样,您就不会隐藏其他您本来不想处理的异常。如果进行这种伪装,您将很难找出问题所在,因此请避免出现这种情况。