【问题标题】:For statement print 3 times instead of 1. Flashscore scrape python对于语句打印 3 次而不是 1 次。 Flashscore scrape python
【发布时间】:2022-01-18 15:10:20
【问题描述】:

我不明白我得到的错误在哪里。我有一个 id 匹配的文件列表,然后如果我写一个 for 来获取每个元素,那么它会输出 3 次 + 每个元素而不是 1。 总输出是 18 行而不是 6 行。

mamm = open('partite2.txt', 'r')
lines = mamm.readlines() 
count = 0 
for x in lines:
    count += 1 
    url = driver.get("https://www.flashscore.com/match/" + x + "/#h2h/overall")  
    time.sleep(3)
    print("Line{}: {}".format(count, x.strip()))

# --- show more results in every section --

    for i in range(3):
        # after every click search all elements again
        section_more_results = driver.find_elements_by_xpath("//div[@class='h2h__section section ']//div[@class='h2h__showMore showMore']")
        #print('len(section_more_results):', len(section_more_results))
        #print('click:', i)
        driver.execute_script('arguments[0].click()', section_more_results[i])
        # it needs to move to this element because it is hidden
        #section_more_results[i].click()

# --- get resulst in every section ---
        
        all_sections = driver.find_elements_by_xpath("//div[@class='h2h__section section ']")

        for section in all_sections:
            all_rows = section.find_elements_by_xpath(".//div[@class='h2h__row']")
            print('--- section ---')
            print('len(all_rows):', len(all_rows))
            count = 0
            c=0
            for row in all_rows:
                date   = row.find_element_by_xpath(".//span[@class='h2h__date']").text
                result = row.find_element_by_xpath(".//span[@class='h2h__regularTimeResult']").text
                team_home = row.find_element_by_xpath(".//span[contains(@class, 'h2h__homeParticipant')]").text
                team_away = row.find_element_by_xpath(".//span[contains(@class, 'h2h__awayParticipant')]").text
                if result != '0 : 0':
                    #print(f"{date} | {result} | {team_home:20} | {team_away}")
                    count += 1
                else:
                    c=c+1
                if count == 7:
                    break


            if c>=1:
                print("NOT OK")
                print("Found: " , c, " 0-0 finished matches")
            else:
                print("OK")

预期的输出应该是:

--- section ---
len(all_rows): 10
OK
--- section ---
len(all_rows): 10
NOT OK
Found:  1  0-0 finished matches
--- section ---
len(all_rows): 10
OK

每个 id 匹配打印 3 行。现在我得到了每个人的 3 倍打印,我不知道为什么。如果我只检查 1 个 id 匹配,这就是我得到的示例。 enter image description here

【问题讨论】:

  • 你有一个 for inside of another for 并且打印在内部 for....我认为这就是你要寻找的问题。

标签: python-3.x selenium


【解决方案1】:

你必须取消缩进最后一个如果,那应该做你想做的事。

    mamm = open('partite2.txt', 'r')
    lines = mamm.readlines()
    count = 0
    for x in lines:
        count += 1
        url = driver.get("https://www.flashscore.com/match/" + x + "/#h2h/overall")
        time.sleep(3)
        print("Line{}: {}".format(count, x.strip()))

        # --- show more results in every section --

        for i in range(3):
            # after every click search all elements again
            section_more_results = driver.find_elements_by_xpath(
                "//div[@class='h2h__section section ']//div[@class='h2h__showMore showMore']")
            # print('len(section_more_results):', len(section_more_results))
            # print('click:', i)
            driver.execute_script('arguments[0].click()', section_more_results[i])
            # it needs to move to this element because it is hidden
            # section_more_results[i].click()

            # --- get resulst in every section ---

            all_sections = driver.find_elements_by_xpath("//div[@class='h2h__section section ']")

            for section in all_sections:
                all_rows = section.find_elements_by_xpath(".//div[@class='h2h__row']")
                print('--- section ---')
                print('len(all_rows):', len(all_rows))
                count = 0
                c = 0
                for row in all_rows:
                    date = row.find_element_by_xpath(".//span[@class='h2h__date']").text
                    result = row.find_element_by_xpath(".//span[@class='h2h__regularTimeResult']").text
                    team_home = row.find_element_by_xpath(".//span[contains(@class, 'h2h__homeParticipant')]").text
                    team_away = row.find_element_by_xpath(".//span[contains(@class, 'h2h__awayParticipant')]").text
                    if result != '0 : 0':
                        # print(f"{date} | {result} | {team_home:20} | {team_away}")
                        count += 1
                    else:
                        c = c + 1
                    if count == 7:
                        break

            if c >= 1:
               print("NOT OK")
               print("Found: ", c, " 0-0 finished matches")
            else:
               print("OK")

【讨论】:

  • 我试过你的代码,但问题是一样的。我的意思是代码应该为每个 id 匹配只打印 3 行,而不是打印 3 部分,因为它会多次迭代它。如何解决?
  • 我之前编辑过代码,帮助大家理解。
猜你喜欢
  • 1970-01-01
  • 2023-01-02
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多