簡體   English   中英

你可以在返回值后繼續遍歷列表嗎?

[英]Can you continue iterating through a list after it has returned a value?

我正在使用 selenium 從 yahoo Finance 抓取信息(我知道 requests 和 bs4 更好,但我想這樣做。)我創建了一個循環 function 從列表中檢索股票代碼符號,但在它返回第一個符號之后它只是在下一次循環迭代中重置並再次返回第一個符號。 我知道 return 應該打破循環,但我不知道任何其他方式來返回我正在循環的值。 我想知道我是否可以向你偉大而強大的神諭尋求幫助!

function被命名為miningLoop。 如果您對此或任何事情有任何疑問,請告訴我?! 我知道我可以將 function 放在主要內部並在那里循環,但是我可以像這樣從主要外部的 function 做到這一點嗎?

class miningCompanies():
    def prime(self):
        self.miningscraper()

    def miningscraper(self):
        while True:
            self.driver = webdriver.Chrome('/Users/admin/Downloads/Chromedriver')
            self.driver.get('https://finance.yahoo.com/quote/' + self.val + '?p=' + self.val + '&.tsrc=fin-srch')
            self.driver.implicitly_wait(5)
            time.sleep(5)
            self.scrapeData()
            time.sleep(5)

    def miningLoop(self):


        companies = ["XOM", "PM", "KXPLF", "PLG", "PLM", 'NCPCF', 'SBUM', 'OTMN', 'SLSR', 'NXXGF',
                     'CAHPF', 'FGOVF', 'ISVLF', 'SGRCF', 'KTGDF', 'KOREF', 'AHELF', 'DOLLF', 'DMEHF', 'TLRS', 'SSVFF',
                     'NEVDF', 'NMTLF', 'AAMMF', 'FFMGF', 'GLDLF', 'ELYGF', 'WLBMF', 'LRTNF', 'ANXGF', 'GPL', 'NAK',
                     'MLRKF', 'RVSDF', 'MUX', 'AUY', 'NGD', 'SLVRF', 'GORO', 'GOLD', 'EGO', 'SLVTF', 'ASX:AZS', 'AAGFF',
                     'ORAGF', 'SILV', 'CCJ', 'UEC', 'DGC.TO', 'OR', 'KGC', 'KOOYF', 'SVM', 'KL', 'JAG', 'TUO', 'BNCH', 'DV', 'ABRA', 'SDR', 'GGI', 'AUY', 'HMY', 'NST',
                     'BTG', 'EVN', 'SAR', 'EDV', 'EQX', 'NG', 'HL', 'PVG', 'SAND', 'CDE', 'MDKA', 'SSRM', 'SILV',
                     'USAS', 'AXU', 'GBR', 'AR', 'HGM', 'GOR', 'WGX', 'LUG', 'HOC', 'POG', 'TXG', 'MAG', 'FSM', 'CEY',
                     'RRL', 'SLR', 'AG', 'AAU', 'TGLDF', 'KLR']

        list = len(companies)
        for x in range(list):
            val = companies[x]
            x += 1


    def scrapeData(self):
        self.driver.implicitly_wait(10)
        time.sleep(2)
        self.PreviousClose = self.driver.find_element_by_xpath('//*[@id="quote-summary"]/div[1]/table/tbody/tr[1]/td[2]/span')
        self.EPS = self.driver.find_element_by_xpath('//*[@id="quote-summary"]/div[2]/table/tbody/tr[4]/td[2]/span')
        self.Market_Cap = self.driver.find_element_by_xpath('//*[@id="quote-summary"]/div[2]/table/tbody/tr[1]/td[2]/span')
        self.PE_Ratio = self.driver.find_element_by_xpath('//*[@id="quote-summary"]/div[2]/table/tbody/tr[3]/td[2]/span')
        self.Dividend = self.driver.find_element_by_xpath('//*[@id="quote-summary"]/div[2]/table/tbody/tr[6]/td[2]')
        self.apiDrive1()
        self.driver.implicitly_wait(10)
        time.sleep(3)
        statistics = self.driver.find_element_by_xpath('//*[@id="quote-nav"]/ul/li[5]/a/span')
        statistics.click()
        self.driver.implicitly_wait(10)
        time.sleep(3)
        self.shareShort = self.driver.find_element_by_xpath('//*[@id="Col1-0-KeyStatistics-Proxy"]/section/div[3]/div[2]/div/div[2]/div/div/table/tbody/tr[7]/td[2]')
        self.institutions = self.driver.find_element_by_xpath('//*[@id="Col1-0-KeyStatistics-Proxy"]/section/div[3]/div[2]/div/div[2]/div/div/table/tbody/tr[6]/td[2]')
        self.revenue = self.driver.find_element_by_xpath('//*[@id="Col1-0-KeyStatistics-Proxy"]/section/div[3]/div[3]/div/div[4]/div/div/table/tbody/tr[1]/td[2]')
        self.high = self.driver.find_element_by_xpath('//*[@id="Col1-0-KeyStatistics-Proxy"]/section/div[3]/div[2]/div/div[1]/div/div/table/tbody/tr[4]/td[2]')
        self.low = self.driver.find_element_by_xpath('//*[@id="Col1-0-KeyStatistics-Proxy"]/section/div[3]/div[2]/div/div[1]/div/div/table/tbody/tr[5]/td[2]')
        self.insiders = self.driver.find_element_by_xpath('//*[@id="Col1-0-KeyStatistics-Proxy"]/section/div[3]/div[2]/div/div[2]/div/div/table/tbody/tr[5]/td[2]')
        self.apiDrive2()
        #self.financial = self.driver.find_element_by_xpath('//*[@id="quote-nav"]/ul/li[8]/a/span')
        #financial.click()
        self.driver.implicitly_wait(3)
        time.sleep(2)
        self.driver.close()

    def apiDrive1(self):
        scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
                 "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]

        creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
        client = gspread.authorize(creds)
        sheet = client.open("Company Break Down").sheet1  # Open the spreadhseet
        data = sheet.get_all_records()
        #first page loop
        x = 106
        i = 1
        while i < x:
            if i == 24 or 43 or 63 or 84 or 102:
                i += 1
            # you have to put a cool down time between requests or Google API will reject you
            self.MC = sheet.update_cell(i, 2, self.Market_Cap.text)
            time.sleep(0.2)
            self.PC = sheet.update_cell(i, 4, self.PreviousClose.text)
            time.sleep(0.2)
            self.PER = sheet.update_cell(i, 19, self.PE_Ratio.text)
            time.sleep(0.2)
            self.EPSC = sheet.update_cell(i, 21, self.EPS.text)
            time.sleep(0.2)
            self.Div = sheet.update_cell(i, 23, self.Dividend.text)
            time.sleep(0.2)
            i += 1
            break


    def apiDrive2(self):
        self.driver.implicitly_wait(10)
        time.sleep(5)
        scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',
                 "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]

        creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
        client = gspread.authorize(creds)
        sheet = client.open("Company Break Down").sheet1  # Open the spreadhseet
        data = sheet.get_all_records()
        x = 106
        i = 1
        while i < x:
            if i == 24 or 43 or 63 or 84 or 102:
                i += 1
            # you have to put a cool down time between requests or Google API will reject you
            self.institution = sheet.update_cell(i, 7, self.institutions.text)
            time.sleep(0.2)
            self.short = sheet.update_cell(i, 28, self.shareShort.text)
            time.sleep(0.2)
            self.rev = sheet.update_cell(i, 21, self.revenue.text)
            time.sleep(0.2)
            self.high52 = sheet.update_cell(i, 34, self.high.text)
            time.sleep(0.2)
            self.insider = sheet.update_cell(i, 8, self.insiders.text)
            time.sleep(0.2)
            self.low52 = sheet.update_cell(i, 33, self.low.text)
            i += 1
            break





beserk = miningCompanies()
beserk.prime()

if i == 24 or 43 or 63 or 84 or 102:這一行實際上是這樣執行的:

if (i == 24) or (43) or (63) or (84) or (102):
    ...

所以它會嘗試評估i == 24 ,如果失敗,它會檢查43是否為 True ,情況總是如此。

相反,您需要為每個數字重寫i == x ,或使用in運算符:

if i in {24, 43, 63, 84, 102}:
    ...

如果這不能解決您的問題,您需要解釋代碼的哪些部分不起作用,以及您期望的結果是什么

回到你原來的問題。 如果您正在遍歷列表並從包含迭代器的 function 返回,那么您將丟失迭代器。 您可能想查看生成器,並使用yield而不是return

暫無
暫無

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

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