【发布时间】:2019-11-04 17:10:42
【问题描述】:
我正在尝试从 Pearson 的网站(从这个网址开始:https://www.pearson.com/us/higher-education/professional---career/computer-science/computer-science.html)获取所有关于计算机科学的书籍,但每个类别的书籍列表都是通过 javascript 生成的。
我尝试使用 Selenium 打开页面,然后使用 BeautifulSoup 对其进行解析。打开类别页面后,我找不到 包含有关一本书的所有信息的标签。
from selenium.webdriver.support import expected_conditions as ec
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
driver = webdriver.Safari()
driver.get('https://www.pearson.com/us/higher-education/professional---career/computer-science/computer-science.html')
wait = WebDriverWait(driver, 2)
content = driver.page_source
soup = BeautifulSoup(content)
#first I loop through categories
categories = list(driver.find_elements_by_xpath('//ul[@class="category-child-list-level-2"]//a'))
for i in range(len(categories)):
print('CATEGORY : {}/170'.format(i+1))
categories[i].click()
while next_page_link != None:
WebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.CLASS_NAME, "content-tile-book-box")))
soup = BeautifulSoup(driver.page_source, 'html.parser')
print(soup.findAll('li', attrs={'class':'content-tile-book-box visible'})) #it results always empty
for a in soup.findAll('li', attrs={'class':'content-tile-book-box visible'}):
#I would like to have access to the books' links
book_title_link = a.find_element_by_xpath('/div[@class="wrap-list-block"]//a')
#loop through all the book pages of the current category
next_page_link = driver.find_element_by_xpath('//a[@aria-label="Next"]')
next_page_link.click()
希望你能帮助我,谢谢!
【问题讨论】:
标签: javascript python selenium web-scraping beautifulsoup