刮页时内部循环重复次数太多。每页约45次

2024-10-04 11:24:26 发布

您现在位置:Python中文网/ 问答频道 /正文

内部for循环重复多次。因此,当我将数据写入csv时,会得到多个数据副本。你知道吗

我使用selenium来点击我的搜索页面,并将每个页面的页面源交给BeautifulSoup,以获取我需要的值。你知道吗

我没有问题得到值,但是输出显示重复3-5次。因此我认为我的循环有问题。你知道吗

我尝试过使用BeautifulSoup对html进行更集中的搜索,以防html中有多个数据实例,但是仍然存在重复。你知道吗

count = 1    
while True:
    try:
        soup = BeautifulSoup(driver.page_source, 'lxml')        
        leadFields = soup.find_all('div', class_='leadFields')
        snippets = soup.find_all('div', class_="snippet ensnippet")

        for l, s in zip(leadFields, snippets):
            print (l.get_text(strip=True) + s.get_text(strip=True))
            print(count)
            count+=1
        nextBtn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.nextItem')))
        action = webdriver.common.action_chains.ActionChains(driver)
        action.move_to_element(nextBtn)
        action.click(nextBtn).perform()
    except TimeoutException:
        break

Tags: 数据divtrueforhtmldrivercountaction
1条回答
网友
1楼 · 发布于 2024-10-04 11:24:26

添加

wait = WebDriverWait(driver, 30)
wait.until(lambda driver: driver.execute_script("return jQuery.active == 0"))

下一次点击后。你知道吗

另外,如果将来有人看到这一点,不要使用TimeoutException来打破一个临时的循环。你知道吗

在评论中大声向pcalkins寻求帮助。你知道吗

相关问题 更多 >