简体   繁体   English

获取网页python的总页数

[英]get total pages number of webpage python

I read a lot on StackOverflow but couldn't find anything that was apt for my situation.我在 StackOverflow 上阅读了很多,但找不到适合我情况的任何内容。

So, I am trying to open a website and search for a keyword.所以,我正在尝试打开一个网站并搜索关键字。 I want to go on every page and get the price.我想在每一页上查看价格。 I will do this for all the pages.我将对所有页面执行此操作。

Initially, I tried clicking on the next button but the 'Next button' on this website doesn't get disabled.最初,我尝试单击下一步按钮,但该网站上的“下一步”按钮没有被禁用。 For example, if there are 15 pages, so on the 15th page the next page button should get disabled, but I can click on it infinite times.例如,如果有 15 页,那么在第 15 页的下一页按钮应该被禁用,但我可以无限次点击它。 After the 15th page, on clicking on it, it just shows the 15th page only.在第 15 页之后,点击它,它只显示第 15 页。

So I am getting the totalpages count and using while loop like follows: Can anyone help me syntactically?所以我得到了总页数并使用 while 循环,如下所示:任何人都可以在语法上帮助我吗? I did the following:我做了以下事情:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

#open a website and search a keyword

from  selenium import webdriver
driver =webdriver.Firefox()

driver.get('https://www.insight.com/en_US/search.html')


list=['printer','printer cartridges','printer accessories']
for searchelement in list:
inputElement = driver.find_element_by_id("search-tab-banner-box-input")
inputElement.clear()
inputElement.send_keys(list)

wait( driver, 30 ).until( EC.visibility_of_element_located( (By.ID, "search-tab-banner-box-input") ) ).send_keys(Keys.ENTER )
inputElement.send_keys(Keys.TAB )

i=1
#Get total number of pages in search result and navigate through
 # I want to do something like this. this is my logic not real code
totalpages= enumerate(driver.gettotalnumber of pages)
while (i<totalpages)

            driver.find_element_by_class_name( "ion-chevron-right" ).click()
            i+=1
#Get total number of pages

How can I get the total number of pages in that variable?如何获得该变量中的总页数?

price='0'  
while True:
     if price!=driver.find_element_by_class_name("prod-price"):
           driver.find_element_by_class_name( "ion-chevron-right" ).click()
     else:
           break
     i+=1
     price=driver.find_element_by_class_name("prod-price")

#Get total number of pages
print('the total number of pages is ', i)

This should work.这应该有效。 It compares the price of the pages.它比较页面的价格。

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

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