豆弁を登って本を読む新刊書の速達(seleniumライブラリ応用)

1273 ワード

Seleniumライブラリアプリケーションテスト

#!/usr/bin/env python3
"""
 
 :https://book.douban.com/
 :Python,selenium
 :Microsoft Edge
 : 、 
"""

from selenium import webdriver


def getContent(marker, tagName):
	#  
	txt = marker.find_element_by_class_name(tagName).get_attribute('textContent').strip()
	return txt


if __name__ == '__main__':
	url = r"https://book.douban.com/"

	driver = webdriver.Edge(r"C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe")
	driver.maximize_window()  #  
	driver.get(url)

	books = []
	uls = driver.find_elements_by_xpath('//ul')

	for ulItem in uls:
		ulItemClass = ulItem.get_attribute('class')
		if ulItemClass == 'list-col list-col5 list-express slide-item':
			lis = ulItem.find_elements_by_xpath('li')

			for liItem in lis:
				divs = liItem.find_element_by_class_name('more-meta')
				bookName = getContent(divs, 'title')
				author = getContent(divs, 'author')
				publishYear = getContent(divs, 'year')
				publisher = getContent(divs, 'publisher')
				books.append([bookName, author, publishYear, publisher])

	driver.quit()		#  
	
	for book in books:
		print(book)