展开和提取文本

问题描述

是否可以从这里使用 Selenium (Python) 单击所有 + 按钮并提取文本值 literature genres codes in Russian。请注意,选项应该一直扩展到底部(有几个级别)?

解决方法

这需要很长时间,所以准备一杯茶,或者看一集你最喜欢的 Netflix 节目 ^^ 但这应该有效:

from selenium import webdriver
import time

d = webdriver.Firefox(executable_path="PATH TO GECKODRIVER")
d.get("http://bbk.rsl.ru/external/bbk?block=ETALON")
time.sleep(1)

# Function to find all new collapsed subnodes after opening a node
def search_sub_nodes(found_elements):
    sub_elements = d.find_elements_by_class_name("tree_node_status_collapsed")
    for sub_element in sub_elements:
        if sub_element not in found_elements:
            sub_element.click()
            found_elements.append(sub_element)
#            time.sleep(0.3) # if needed use time.sleep to ensure loading
            search_sub_nodes(sub_elements.copy())

# Main Function to open all nodes,calls search_sub_nodes to open child nodes
def open_all_tree_nodes():
    elements = d.find_elements_by_class_name("tree_node_status_collapsed")
    for element in elements:
        element.click()
#        time.sleep(0.3) # if needed use time.sleep to ensure loading
        search_sub_nodes(elements.copy())

open_all_tree_nodes()
# open_all_tree_nodes() # add second run just to be sure all nodes are opened

titles = []
# Find all tr elements under class 'node_own_area' (those contain the title)
elements = d.find_elements_by_css_selector(".node_own_area tr")
for element in elements:
    # get the title property and add it to titles list
    titles.append(str(element.get_property("title")))

# print(titles) debug
# write titles to txt file
with open("title_file.txt","w") as f:
    f.writelines(titles)

这可能可以简化一点,但我不是python专家^^

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...