from selenium.webdriver import Chrome from selenium.webdriver.common.by import By with Chrome() as driver: list_novel = [] driver.get("https://top.baidu.com/board?tab=novel") list_ele = driver.find_elements(By.XPATH, '//div[@class="category-wrap_iQLoo "]') for item in list_ele: dict_novel = {} dict_novel["name"] = item.find_element(By.XPATH, './/div[@class="c-single-text-ellipsis"]').text dict_novel["hot"] = item.find_element(By.XPATH, './/div[@class="hot-index_1Bl1a"]').text details = item.find_elements(By.XPATH, './div/div') for i in range(len(details)): if i == 0: dict_novel["author"] = details[i].text elif i == 1: dict_novel["type"] = details[i].text elif i == 2: dict_novel["brief"] = details[i].text list_novel.append(dict_novel) for item in list_novel: print(item)
标签:novel,python,text,list,爬取,item,dict,details,百度 From: https://www.cnblogs.com/xmgcool/p/16933765.html