Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我一直收到缩进错误,我不应该';不可能_Python_Selenium_Beautifulsoup_Urllib2_Xlsxwriter - Fatal编程技术网

Python 我一直收到缩进错误,我不应该';不可能

Python 我一直收到缩进错误,我不应该';不可能,python,selenium,beautifulsoup,urllib2,xlsxwriter,Python,Selenium,Beautifulsoup,Urllib2,Xlsxwriter,每当我在CMD中运行我的程序时,就会出现缩进错误。对我来说,整个程序的缩进看起来很完美,所以我完全不知道为什么会收到错误 CMD错误: scraper9.py", line 50 browser.get(url2) ^ IndentationError: unexpected unindent 我已经完全删除了所有缩进,并一行一行地重新缩进,以到达当前迭代,但我仍然会出错 导入操作系统 导入系统 导入csv 从bs

每当我在CMD中运行我的程序时,就会出现缩进错误。对我来说,整个程序的缩进看起来很完美,所以我完全不知道为什么会收到错误

CMD错误:

        scraper9.py", line 50
            browser.get(url2)
            ^
        IndentationError: unexpected unindent
我已经完全删除了所有缩进,并一行一行地重新缩进,以到达当前迭代,但我仍然会出错

导入操作系统 导入系统 导入csv 从bs4导入BeautifulSoup 导入urllib2 导入xlsxwriter 从selenium导入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")
我希望代码执行时不会出现错误


它出错了。我太迷路了。

你的
尝试:
缺少
,除了:
最后:

            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)
试试看

顺便说一句,您应该减少
try:
子句中的代码大小,只捕获您正在处理的异常。在您的情况下,
属性错误(如果
.contents
失败)将被第一个
捕获,除了:
。更好:

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]
对于外部的
尝试类似的方法
/
,除了


这样,您就不会掩盖您从未打算处理的其他异常。如果你这样做,你将很难找出哪里出了问题,所以要避免它。

你的
尝试:
缺少
,除了:
最后:

            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)
试试看

顺便说一句,您应该减少
try:
子句中的代码大小,只捕获您正在处理的异常。在您的情况下,
属性错误(如果
.contents
失败)将被第一个
捕获,除了:
。更好:

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]
对于外部的
尝试类似的方法
/
,除了


这样,您就不会掩盖您从未打算处理的其他异常。如果你这样做,你将很难找出哪里出了问题,所以要避免它。

你是在混合空格和制表符吗?尝试使用编辑器/IDE的工具规范化空白。是否混合了空格和制表符?尝试使用编辑器/IDE的工具来规范化空白。哇,该死。这里有一堂关于编码的迷你课。非常感谢你!哇,该死。这里有一堂关于编码的迷你课。非常感谢你!