如何使用随机链接创建页面计数器

问题描述 投票:0回答:1

我想为洗牌链接创建页面倒计时,以便轻松了解我的脚本实际进展的位置。

我尝试:

def page_counter():
  for x in range(1000):
      yield x

count = page_counter()
driver = webdriver.Chrome()
driver.get('https://www.betstar.com.au/sports/soccer/')
elements = [x.get_attribute("href") for x in
driver.find_elements_by_css_selector(".market-group a")]
 shuffle(elements)
print(len(elements))

import operator

links = dict((next(count) + 1, e) for e in elements)
desc_links = sorted(links.items(), key=operator.itemgetter(1))

for link in desc_links:
    driver.get(link[1])
    print(f'At Page: {link[0]}')

电流输出(随机数):

   64
    At Page: 44
    At Page: 24
    At Page: 34
    At Page: 46
    At Page: 42
    At Page: 10

以上内容对于了解它的进展情况并不十分有用。改成:

driver.get('https://stackoverflow.com/questions')

改成:

 driver.find_elements_by_css_selector("#questions .question-hyperlink")]

我得到了(这也是我渴望的输出,因为它倒计时)

15
At Page: 15  #When at page 15
At Page: 14   #When at page 15
At Page: 13  #When at page 15
At Page: 12
At Page: 11
At Page: 10
At Page: 9

如何使用随机链接创建页面计数器?

我也尝试过这个工作:

driver = webdriver.Chrome()


driver.get('https://www.betstar.com.au/sports/soccer/')
elements = driver.find_elements_by_css_selector(".market-group a")
elem_href1 = [element.get_attribute("href") for element in elements]
print(elem_href1)
print (len(elem_href1))
shuffle(elem_href1)
for link in elem_href1:#(2)
    driver.get(link)
    print(len(link))
    import numbers

    #number = number -= 1
    #print (len(elem_href1-(number)))



print (len(elem_href1)) gives total number of pages to navigate to.


print(len(link)) gives random number due to shuffle.

不支持,虽然链接被洗牌,但无法知道脚本是如何进展的

如何创建页面计数器以显示所需的输出。

python python-3.x selenium selenium-webdriver
1个回答
0
投票

试试这个

更换

links = dict((next(count) + 1, e) for e in elements)
desc_links = sorted(links.items(), key=operator.itemgetter(1))

for link in desc_links:
    driver.get(link[1])
    print(f'At Page: {link[0]}')

import collections
links = dict((next(count) + 1, e) for e in elements)

desc_links = collections.OrderedDict(sorted(links.items(), reverse=True))

for key, value in desc_links.items():
    driver.get(value)
    print('At Page: ' + str(key))
© www.soinside.com 2019 - 2024. All rights reserved.