我写了一个小应用程序,它从AWS站点获取保留实例的价格,然后打印实例的名称和价格(我只希望有可转换的3年期限)。应用程序工作。但是,它的运行速度非常慢,可能是因为列表等位基因包含1925个元素,稍后我将遍历它的所有元素。我想像在代码中那样过滤数据(让我们只取名称以c5开头的Linux实例)。如何做得更快?有没有任何机会加快过滤的地方,不要把一切从这个网站到等位基因列表?提前感谢您的帮助!你知道吗
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import time
caps = DesiredCapabilities().FIREFOX
#caps["pageLoadStrategy"] = "normal" # complete
#caps["pageLoadStrategy"] = "eager" # interactive
caps["pageLoadStrategy"] = "none"
browser = webdriver.Firefox(desired_capabilities=caps)
browser.get('https://aws.amazon.com/ec2/pricing/reserved-instances/pricing/')
delay=3
time.sleep(10)
#browser.find_element_by_link_text('Windows').click()
try:
myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.CLASS_NAME, 'aws-plc-content')))
print ("Page is ready!")
except TimeoutException:
print ("Loading took too much time!")
time.sleep(2)
allElements=browser.find_elements_by_class_name("aws-pricing-table-wrapper")
for el in allElements:
lista=el.text.split("\n")
indeks=lista.index("CONVERTIBLE 3-YEAR TERM")
prices=lista[indeks+2]
if lista[0].startswith('c5'):
print(lista[0])
print(prices.split()[4])
我已经尝试了Chrome驱动程序,我希望我们也能在FF上得到同样的结果。你知道吗
要做到这一点,你需要做几件事。你知道吗
WebDriverWait
查找所有元素并追加到列表中,同时检查列表中没有重复项element.get_attribute("textContent")
获取值如果使用element.text
可能会得到一些空字符串。你知道吗试试下面的代码。你知道吗
控制台上的输出:
解决方案2:
如果你去网络标签,你会得到以下API
返回
json()
格式的结果。你知道吗输出:
相关问题 更多 >
编程相关推荐