解析硒、滚动时出现问题
有问题,我会解释一下代码的脚本。
首先,我们进入主页,然后 for 循环进入第 1 页,滚动并解析照片的链接(不滚动就不会解析),然后进入第 2 页,已经出现问题了。 它会在一秒钟内滚动到底部。我需要它逐渐滚动,就像在第一页上一样。 我抛出代码:
driver.get(url=url)
n = 0
urla = driver.find_element(By.CLASS_NAME, "ipsPagination_pageJump").text
for page_number in range(int(urla.split()[3])):
page_number = page_number + 1
driver.get(url=url + f"page/{page_number}")
time.sleep(2)
imgs = driver.find_elements(By.CLASS_NAME, "cGalleryPatchwork_image")
for i in imgs:
n = n + 500
driver.execute_script(f"window.scrollTo(0, {n})")
time.sleep(0.2)
print(i.get_attribute("src"))
driver.quit()
我知道代码很糟糕并且没有优化
there is a problem, I will explain the script of the code.
First, we go to the main page, then the for loop that goes to page 1, scrolls and parses the links to the photo (it doesn’t parse without scrolling), then goes to page 2, and there is already a problem.
It scrolls to the bottom in a second. And I need it to scroll gradually, like on the first page.
I throw the code:
driver.get(url=url)
n = 0
urla = driver.find_element(By.CLASS_NAME, "ipsPagination_pageJump").text
for page_number in range(int(urla.split()[3])):
page_number = page_number + 1
driver.get(url=url + f"page/{page_number}")
time.sleep(2)
imgs = driver.find_elements(By.CLASS_NAME, "cGalleryPatchwork_image")
for i in imgs:
n = n + 500
driver.execute_script(f"window.scrollTo(0, {n})")
time.sleep(0.2)
print(i.get_attribute("src"))
driver.quit()
I know the code is very bad and not optimized
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

要逐渐滚动一个元素一个元素,您应该使用以下
execute_script
命令driver.execute_script("arguments[0].scrollIntoView(true);", i )
代码:
to scroll gradually one element by one element you should use the following
execute_script
commanddriver.execute_script("arguments[0].scrollIntoView(true);", i)
Code: