【问题标题】:get total pages number of webpage python获取网页python的总页数
【发布时间】:2017-08-11 14:49:03
【问题描述】:

我在 StackOverflow 上阅读了很多内容,但找不到适合我情况的任何内容。

所以,我正在尝试打开一个网站并搜索关键字。我想浏览每一页并获得价格。我将为所有页面执行此操作。

最初,我尝试单击“下一步”按钮,但该网站上的“下一步”按钮并未被禁用。例如,如果有 15 页,那么在第 15 页上,下一页按钮应该被禁用,但我可以无限次单击它。在第15页之后,点击它,它只显示第15页。

所以我得到了总页数并使用如下的 while 循环:任何人都可以在语法上帮助我吗?我做了以下事情:

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

如何获取该变量中的总页数?

【问题讨论】:

  • 您的while 语句不应缩进
  • 哦!对不起,这里打错了。
  • driver.gettotalnumber of pages?
  • page_source比较网页的html怎么样
  • @whackamadoodle3000:这是我的逻辑。不是真正的代码

标签: python selenium


【解决方案1】:
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)

这应该可以。它比较页面的价格。

【讨论】:

  • 嗯,我想知道我应该如何实现>>>>>>>>>>>>>totalpages= enumerate(driver.gettotalnumber of pages)
  • gettotalnumber 不是函数
  • i会有页数
  • 是的,这就是我的问题!将这些关键字发送到搜索栏后,如何获取该网站上的总页数。
  • 我编辑了答案。现在它只是获取页数并打印它。这段代码只会得到数字页,而不是其余的东西
猜你喜欢
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 2011-09-20
  • 1970-01-01
  • 2010-12-23
  • 2020-03-19
相关资源
最近更新 更多