我正在使用Selenium从Twitter抓取关注者名称,并且该页面是无限的,每当向下滚动时,我都可以看到新的关注者。 我想以某种方式转到页面底部,以便我可以删除所有关注者。
while number != 5:
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
number = number + 1
time.sleep(5)
usernames = driver.find_elements_by_class_name(
"css-4rbku5.css-18t94o4.css-1dbjc4n.r-1loqt21.r-1wbh5a2.r-dnmrzs.r-1ny4l3l")
for username in usernames:
print(username.get_attribute("href"))
现在,代码正在滚动5次。我输入了一个静态值,但我不知道需要多少滚动才能到达页面底部。
答案 0 :(得分:0)
使用以下代码进行无限加载。它将一直滚动直到加载新元素,即页面大小正在更改。
# Get scroll height after first time page load
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(2)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
答案 1 :(得分:0)
在以下脚本中,没有睡眠时间,因此滚动速度更快:
SCROLL_PAUSE_TIME = 4
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
import datetime
time_past = datetime.datetime.now()
while (datetime.datetime.now() - time_past).seconds <=SCROLL_PAUSE_TIME:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height