【发布时间】:2019-03-13 20:25:01
【问题描述】:
我的目标:收集用户在可汗学院完成的项目数量。
为此,我需要解析个人资料用户页面。但我需要点击show more 来查看用户所做的所有项目,然后将它们刮掉。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException,StaleElementReferenceException
from bs4 import BeautifulSoup
# here is one example of a user
driver = webdriver.Chrome()
driver.get('https://www.khanacademy.org/profile/trekcelt/projects')
# to infinite click on show more button until there is none
while True:
try:
showmore_project=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME,'showMore_17tx5ln')))
showmore_project.click()
except TimeoutException:
break
except StaleElementReferenceException:
break
# parsing the profile
soup=BeautifulSoup(driver.page_source,'html.parser')
# get a list of all the projects
project=soup.find_all(class_='title_1usue9n')
# get the number of projects
print(len(project))
此代码为print(len(project)) 返回0。这不正常,因为当您手动检查 https://www.khanacademy.org/profile/trekcelt/projects 时,您会看到那里的项目数量肯定不是 0。
奇怪的事情:首先,你可以看到(使用 webdriver)这段代码运行良好,然后 selenium 点击了 show more button 以外的其他东西,它点击了项目的链接之一,因此发生了变化这就是我们得到0的原因。
我不明白如何更正我的代码,所以 selenium 只点击右键,没有别的。
【问题讨论】:
-
当我运行你的代码时,它返回 381。
-
尝试 showmore=WebDriverWait(driver, 10).until(EC.presence_of_elements_located((By.CLASS_NAME,'showMore_17tx5ln'))) showmore[0].click()
-
另外,尝试点击后等到整个页面渲染完毕,使用BeautifulSoup获取html代码。
-
@mm_ 我收到此错误消息
showmore_project[0].click() TypeError: 'WebElement' object does not support indexing -
@mm_ 我的问题不是渲染时间,而是点击功能,当我运行我的代码时,它点击显示更多按钮,然后点击不同的链接,因此无法解析漂亮的汤我想要的页面。
标签: python python-3.x selenium web-scraping beautifulsoup