我想为洗牌链接创建页面倒计时,以便轻松了解我的脚本实际进展的位置。
我试试:
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.
不支持,虽然链接被洗牌,但无法知道脚本是如何进展的
如何创建页面计数器以显示所需的输出。
答案 0 :(得分: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))