加速硒污染

2024-05-18 04:28:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个小应用程序,它从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])

Tags: 实例fromimportbrowserawsbytimeselenium
1条回答
网友
1楼 · 发布于 2024-05-18 04:28:23

我已经尝试了Chrome驱动程序,我希望我们也能在FF上得到同样的结果。你知道吗

要做到这一点,你需要做几件事。你知道吗

  1. 无限循环并首先滚动页面
  2. 使用WebDriverWait查找所有元素并追加到列表中,同时检查列表中没有重复项
  3. 一旦它到达循环的底部,它就会从循环中断开。你知道吗
  4. 使用下面的XPATH提供您想要的输出。你知道吗
  5. 使用element.get_attribute("textContent")获取值如果使用element.text可能会得到一些空字符串。你知道吗

试试下面的代码。你知道吗

from selenium import webdriver
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


browser = webdriver.Chrome()
browser.get('https://aws.amazon.com/ec2/pricing/reserved-instances/pricing/')
delay=10

try:
    myElem = WebDriverWait(browser, delay).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.aws-plc-content')))
    print ("Page is ready!")
except TimeoutException:
    print ("Loading took too much time!")

last_height = browser.execute_script("return document.body.scrollHeight")
items=[]
while True:
    browser.find_element_by_tag_name('body').send_keys(Keys.END)
    time.sleep(1)

    allElements = WebDriverWait(browser, 20).until(EC.presence_of_all_elements_located(
        (By.XPATH, "//div[@class='aws-pricing-table-wrapper']/h2[starts-with(text(),'c5.')]")))
    print(len(allElements))
    for el in allElements:

        if el.text in items:
            continue

        items.append(el.get_attribute("textContent").strip())
        items.append(el.find_element_by_xpath("./following-sibling::table[4]//tr//th[contains(.,'Convertible 3-Year Term')]/following::tbody[1]//tr[1]//td[4]").get_attribute("textContent").strip())

    new_height = browser.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height

#Print all items and their price.
print(items)
#Get the length of the list #it should be 244X2
print(len(items))

控制台上的输出:

Page is ready!
9
88
['c5.large', '$0.041', 'c5.xlarge', '$0.081', 'c5.2xlarge', '$0.162', 'c5.4xlarge', '$0.324', 'c5.9xlarge', '$0.729', 'c5.12xlarge', '$0.985', 'c5.18xlarge', '$1.459', 'c5.24xlarge', '$1.970', 'c5.metal', '$1.970', 'c5.large', '$0.101', 'c5.xlarge', '$0.141', 'c5.2xlarge', '$0.292', 'c5.4xlarge', '$0.454', 'c5.9xlarge', '$0.859', 'c5.12xlarge', '$1.115', 'c5.18xlarge', '$1.589', 'c5.24xlarge', '$2.100', 'c5.metal', '$2.100', 'c5.large', '$0.074', 'c5.xlarge', '$0.114', 'c5.2xlarge', '$0.195', 'c5.4xlarge', '$0.357', 'c5.9xlarge', '$0.762', 'c5.12xlarge', '$1.018', 'c5.18xlarge', '$1.492', 'c5.24xlarge', '$2.003', 'c5.metal', '$2.003', 'c5.large', '$0.133', 'c5.xlarge', '$0.265', 'c5.2xlarge', '$0.530', 'c5.4xlarge', '$1.060', 'c5.9xlarge', '$2.385', 'c5.12xlarge', '$3.193', 'c5.18xlarge', '$4.771', 'c5.24xlarge', '$6.386', 'c5.metal', '$6.386', 'c5.large', '$0.613', 'c5.xlarge', '$0.745', 'c5.2xlarge', '$1.490', 'c5.4xlarge', '$2.980', 'c5.9xlarge', '$6.705', 'c5.12xlarge', '$8.953', 'c5.18xlarge', '$13.411', 'c5.24xlarge', '$17.906', 'c5.metal', '$17.906', 'c5.large', '$0.200', 'c5.xlarge', '$0.333', 'c5.2xlarge', '$0.665', 'c5.4xlarge', '$1.331', 'c5.9xlarge', '$2.994', 'c5.12xlarge', '$4.004', 'c5.18xlarge', '$5.988', 'c5.24xlarge', '$8.008', 'c5.metal', '$8.008', 'c5.xlarge', '$1.765', 'c5.2xlarge', '$3.530', 'c5.4xlarge', '$7.060', 'c5.9xlarge', '$15.885', 'c5.12xlarge', '$21.193', 'c5.18xlarge', '$31.771', 'c5.24xlarge', '$42.386', 'c5.metal', '$42.386', 'c5.large', '$0.521', 'c5.xlarge', '$0.561', 'c5.2xlarge', '$1.122', 'c5.4xlarge', '$2.244', 'c5.9xlarge', '$5.049', 'c5.12xlarge', '$6.745', 'c5.18xlarge', '$10.099', 'c5.24xlarge', '$13.490', 'c5.metal', '$13.490', 'c5.large', '$0.108', 'c5.xlarge', '$0.149', 'c5.2xlarge', '$0.297', 'c5.4xlarge', '$0.595', 'c5.9xlarge', '$1.338', 'c5.12xlarge', '$1.796', 'c5.18xlarge', '$2.676', 'c5.24xlarge', '$3.592', 'c5.metal', '$3.592', 'c5.xlarge', '$1.581', 'c5.2xlarge', '$3.162', 'c5.4xlarge', '$6.324', 'c5.9xlarge', '$14.229', 'c5.12xlarge', '$18.985', 'c5.18xlarge', '$28.459', 'c5.24xlarge', '$37.970', 'c5.metal', '$37.970']
176

解决方案2:

如果你去网络标签,你会得到以下API

https://a0.p.awsstatic.com/pricing/1.0/ec2/region/us-east-2/reserved-instance/linux/index.json?

返回json()格式的结果。你知道吗

import requests
res=requests.get("https://a0.p.awsstatic.com/pricing/1.0/ec2/region/us-east-2/reserved-instance/linux/index.json?",verify=False).json()
for item in res['prices']:

    if 'c5.' in item['attributes']['aws:ec2:instanceType']:
        if item['attributes']['aws:offerTermLeaseLength']=="3yr" and item['attributes']['aws:offerTermOfferingClass'] =="convertible" and item['attributes']['aws:offerTermPurchaseOption']=="No Upfront":
           print(item['attributes']['aws:ec2:instanceType'])
           print('$' + str(item['calculatedPrice']['effectiveHourlyRate']['USD']))

输出

c5.xlarge
$0.08100000000000002
c5.18xlarge
$1.4589999999999999
c5.4xlarge
$0.32400000000000007
c5.2xlarge
$0.16200000000000003
c5.24xlarge
$1.97
c5.9xlarge
$0.729
c5.metal
$1.97
c5.12xlarge
$0.985
c5.large
$0.041

enter image description here

相关问题 更多 >