【发布时间】:2017-01-25 11:54:46
【问题描述】:
html looks like this我已经编写了这段代码来从一个 url 中抓取所有课程。为此,我正在尝试使用xpath 计算课程数量。但它没有给我任何东西。我哪里做错了?
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait`
class FinalProject:
def __init__(self,url= "https://www.class-central.com/subject/data-science"):`
self.url = url
base_url = 'https://www.class-central.com'
self.error_flag = False
self.driver = webdriver.Chrome(<path to chromedriver>)
self.driver.get(self.url)
sleep(2)
self.count_course_and_scroll()
def count_course_and_scroll(self):
wait = WebDriverWait(self.driver, 30);
ele = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'Not right now, thanks.')));
ele.click()
print "----------------------POP UP CLOSED---------------------"
total_courses = self.driver.find_element_by_xpath("//span[@id='number-of-courses']")
print total_courses
print total_courses.text
self.driver.close()
fp = FinalProject()
【问题讨论】:
-
请分享您工作的html代码。
-
你有什么错误吗?
-
@Jose 我已经分享了 html 的截图。我不知道我应该如何分享完整的。
-
@Guy No errors
<selenium.webdriver.remote.webelement.WebElement (session="af387306f394aab0b195eb03399f4a0c", element="0.13137647096001892-2")>是total_courses的输出,total_courses.text给出空白字符串。 -
@python_user,您应该将代码共享为文本,而不是图像。还要编辑您的代码,因为存在一些缩进问题
标签: python-2.7 selenium xpath selenium-chromedriver