http://www.treejs.cn/v3/demo/cn/core/simpleData.html
目前只能理出简单爬已经展开的节点。模拟点击一层层加载怎么实现?
''''
def get_html_src(url):
driver = webdriver.Chrome("/Users/qk/Downloads/chromedriver")
driver.get(url)
parentElement = driver.find_element_by_id('treeDemo')
elementList = parentElement.find_elements_by_tag_name("li")
with open("tmp/ztree.csv", "a") as csvfile:
writer = csv.writer(csvfile)
for e in elementList:
writer.writerow([e.text])
e.find_elements_by_tag_name
print('',)
print('节点名称:', e.text)
csvfile.close()
time.sleep(10)
driver.close()
if __name__ == "__main__":
get_html_src('http://www.treejs.cn/v3/demo/cn/core/simpleData.html')
''''
1
dibis OP 修改好了
with open("tmp/ztree.csv", "a") as csvfile: writer = csv.writer(csvfile) for num,e in enumerate(elementList,start=1): writer.writerow([e.text]) try: print(num) print(e.get_attribute('innerHTML')) e.find_element_by_class_name('center_close').click() time.sleep(2) #print(eparent) #eparent.click() except WebDriverException: print("Element is not clickable") print(e.text) |
2
dibis OP 下面要改成递归方法和叫错
|