简体   繁体   English

Append 不会在 Python 中“循环”我的搜索代码

[英]Append does not “loop” my search code in Python

I`m trying to make a code that search (number code called 'cpf') and return me the infos that the url will show.我正在尝试制作一个搜索代码(称为“cpf”的数字代码)并向我返回 url 将显示的信息。 The code works properly with a single cpf, but the append function seens not to work correctly...该代码可在单个 cpf 上正常工作,但 append function 似乎无法正常工作......

    def search_cpfs(self):
    names = []
    consigs = []
    cards = []
    for cpf in self.cpfs:
        print(f"Procurando {cpf}.")

        self.driver.get(self.bot_url)

        cpf_input = self.driver.find_element_by_xpath('//*[@id="search"]/div/div[1]/input')
        cpf_input.send_keys(cpf)

        time.sleep(2)

        cpfButton = self.driver.find_element_by_xpath('//*[@id="search"]/div/div[2]/button')
        cpfButton.click()

        time.sleep(2)

        name = self.driver.find_element_by_xpath("/html/body/main[1]/div[1]/div[1]/div[1]/div[1]/h2").text
        consig = self.driver.find_element_by_xpath("/html/body/main[1]/div[1]/div[1]/div[3]/div[2]/span").text
        card = self.driver.find_element_by_xpath("/html/body/main[1]/div[1]/div[1]/div[3]/div[3]/span").text

        names.append(name)
        consigs.append(consig)
        cards.append(card)

        print(name, consig, card)

        return names, consigs, cards

Perhaps due to indentation?也许是因为缩进? Your current code shows/returns only one/the first result.您当前的代码仅显示/返回一个/第一个结果。

Try this:尝试这个:

def search_cpfs(self):
    names = []
    consigs = []
    cards = []
    for cpf in self.cpfs:
        print(f"Procurando {cpf}.")

        self.driver.get(self.bot_url)

        cpf_input = self.driver.find_element_by_xpath('//*[@id="search"]/div/div[1]/input')
        cpf_input.send_keys(cpf)

        time.sleep(2)

        cpfButton = self.driver.find_element_by_xpath('//*[@id="search"]/div/div[2]/button')
        cpfButton.click()

        time.sleep(2)

        name = self.driver.find_element_by_xpath("/html/body/main[1]/div[1]/div[1]/div[1]/div[1]/h2").text
        consig = self.driver.find_element_by_xpath("/html/body/main[1]/div[1]/div[1]/div[3]/div[2]/span").text
        card = self.driver.find_element_by_xpath("/html/body/main[1]/div[1]/div[1]/div[3]/div[3]/span").text

        names.append(name)
        consigs.append(consig)
        cards.append(card)

        print(name, consig, card)

    return names, consigs, cards

I assume the function is within a class.我假设 function 在 class 内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM