无法将这两个表达式放入我的脚本中 [英] Unable to fit the two expressions into my script
本文介绍了无法将这两个表达式放入我的脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个脚本,使用python和硒结合从网页上抓取文档.但是,我卡住的唯一一件事就是打印值.由于硒不支持文本索引,因此我无法进一步考虑实现此目的.查看我的代码,您将了解我的意思.我已注释掉需要纠正的两行内容.提前致谢.到目前为止,这是我写的内容:
I've written a script to scrape documents from a web page using python in combination with selenium. However, the only thing I got stuck is print the value. As selenium doesn't support indexing in text, I can't think further to accomplish this. Taking a look into my code You will get to know what I meant. I've commented out the two lines to be rectified. Thanks in advance. Here is what I've written so far:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www.aopa.org/learntofly/school/')
driver.find_element_by_id('searchTerm').send_keys('All')
time.sleep(5)
driver.find_element_by_id('btnSearch').click()
time.sleep(3)
for items in driver.find_elements_by_xpath('//td/a'):
driver.execute_script(items.get_attribute('href'))
time.sleep(3)
for docs in driver.find_elements_by_xpath('//div[@id="schoolDetail"]'):
print(docs.text)
# Instead of docs I wanted to print the two expressions but can't
# Name = docs.find_elements_by_xpath('//div[@id="schoolDetail"]/text()[1]')
# Address = docs.find_elements_by_xpath('//div[@id="schoolDetail"]/text()[2]')
# print(Name, Address)
for back_links in driver.find_elements_by_xpath('//div[@id="schoolDetail"]//h4/a'):
driver.execute_script(back_links.get_attribute('href'))
driver.quit()
推荐答案
尝试下面的代码,如有任何问题,请通知我:
Try below code and let me know in case of any issues:
Name = driver.execute_script('return arguments[0].childNodes[1].textContent', docs) # To get Name text value
Address = driver.execute_script('return arguments[0].childNodes[3].textContent', docs) # To get Address text value
这篇关于无法将这两个表达式放入我的脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文