我正在尝试编写一个python脚本,该脚本将转到youtube频道。单击“视频”选项卡。然后在网页上搜寻内容。我完全可以刮内容,直到。。。我找到“加载更多”按钮。我的python脚本成功地单击了load more按钮一次,但它再也没有按下它:'(我如何修改代码,我必须让它一次又一次地单击它,直到它不存在为止)。这样,我可以打开用户的完整频道,从他们拥有的每一个视频中获取信息。谢谢你。from selenium import webdriver

from selenium.common.exceptions import NoSuchElementException

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

^^这些是我导入的模块。我也不知道如何在代码中添加NoSuchElementException。代码如下:chrome_path = r"/Users/jack/Desktop/Other/Downloads/Software_and_Programs/chromedriver"

browser = webdriver.Chrome(chrome_path)

YOUTUBER_HOME_PAGE_URL = "https://www.youtube.com/user/Google/videos"

PATIENCE_TIME = 60

LOAD_MORE_BUTTON_XPATH = '//*[@id="browse-itemsprimary"]/li[2]/button/span/span[2]'

def waitForLoad(inputXPath):

Wait = WebDriverWait(browser, PATIENCE_TIME)

Wait.until(EC.presence_of_element_located((By.XPATH, inputXPath)))

loadMoreButtonExists = True

while loadMoreButtonExists:

try:

waitForLoad(LOAD_MORE_BUTTON_XPATH)

WebDriverWait(browser, PATIENCE_TIME)

loadMoreButton = browser.find_element_by_partial_link_text('Load More')

#loadMoreButton = browser.find_element_by_xpath(LOAD_MORE_BUTTON_XPATH)

loadMoreButton.click()

except:

print 'we have completely loaded every video from this Youtuber. Now we will scrape the video content\n'

loadMoreButtonExists = False

我已经使用了xpath方法,但这似乎仍然不起作用。我已经在上面的代码中注释掉了。如果我能在这件事上得到帮助那就太棒了。我找不到任何好的答案。我相信可以用硒来做,但如果不行,我应该用什么呢?

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐