import requests
from lxml import etree
import json
url='https://azure.microsoft.com/zh-cn/pricing/details/virtual-machines/linux/'
resp = requests.get(url)
resp = etree.HTML(resp.text)
options = resp.xpath('//select[@id="region-selector"]//option')
mark_item = {}
for option in options:
mark_item[''.join(option.xpath('./@value'))] = ''.join(option.xpath('./text()'))
def getprice(json_str, quyu):
"""
:param json_str:
:param quyu: 区域地址
:return:
"""
try:
price = json.loads(json_str)['regional']
# return "$"+str(round(price[quyu],4))+"/小时 and $"+str(round(float(price[quyu])*365*24/12,4))+"/月"
return round(price[quyu], 4), round(float(price[quyu]) * 365 * 24 / 12, 4)
except:
return '– –'
for k, v in mark_item.items():
item = {}
item['区域'] = v
kinds = resp.xpath('//div[@class="row row-size1 column series"]')
for kind in kinds:
item['类别'] = ''.join(kind.xpath('.//h2[@class="text-heading3"]/text()')).strip()
all_xilie = kind.xpath('./div[@id]/div')
# print('长度',len(all_xilie))
for xilie in all_xilie:
item['系列'] = ''.join(xilie.xpath('.//h3/text()')).strip()
# print(item['类别'],item['系列'])
# # 第一个是添加到估计
mark=0
table_heads = xilie.xpath('.//table[@class="sd-table"]/thead/tr/th')
table_heads = [''.join(i.xpath('.//text()')).strip().replace(' ', '') for i in table_heads]
if table_heads[0] == '添加到估计':
table_heads = table_heads[1:]
mark = 1
# print(table_heads)
#
table_trs = xilie.xpath('.//table[@class="sd-table"]/tbody/tr')
cz = []
for tr in table_trs:
if mark == 1:
vals = [i.strip() for i in tr.xpath('.//td/text()')[2:]]
else:
vals = [i.strip() for i in tr.xpath('.//td/text()')]
price1 = ''.join(tr.xpath('.//td[6]/span/@data-amount'))
price2 = ''.join(tr.xpath('.//td[7]/span/@data-amount'))
price3 = ''.join(tr.xpath('.//td[8]/span/@data-amount'))
price4 = ''.join(tr.xpath('.//td[9]/span/@data-amount'))
for i in range(len(table_heads)):
item[table_heads[i]] = vals[i]
item['现用现付'] = getprice(price1, k)
if item['现用现付'] == '– –':
item['现用现付'] = '定价在所选区域中不可用'
if item['现用现付'] != '定价在所选区域中不可用':
item['保留1年(节省百分比)'] = getprice(price2, k)
item['保留3年(节省百分比)'] = getprice(price3, k)
if price4:
item['保留3年Azure混合权益(节省百分比)'] = getprice(price4, k)
else:
item['保留3年Azure混合权益(节省百分比)'] = '– –'
if item['保留1年(节省百分比)'] != '– –':
item['保留1年(节省百分比)'] = '${}/小时 or $ {}/月 (~{}%)'.format(item['保留1年(节省百分比)'][0],
item['保留1年(节省百分比)'][1], 100 - int(
(item['保留1年(节省百分比)'][0] / item['现用现付'][0]) * 100))
if item['保留3年(节省百分比)'] != '– –':
item['保留3年(节省百分比)'] = '${}/小时 or $ {}/月 (~{}%)'.format(item['保留3年(节省百分比)'][0],
item['保留3年(节省百分比)'][1], 100 - int(
(item['保留3年(节省百分比)'][0] / item['现用现付'][0]) * 100))
if item['保留3年Azure混合权益(节省百分比)'] != '– –':
item['保留3年Azure混合权益(节省百分比)'] = '${}/小时 or $ {}/月 (~{}%)'.format(item['保留3年Azure混合权益(节省百分比)'][0],
item['保留3年Azure混合权益(节省百分比)'][1],
100 - int(
(item[
'保留3年Azure混合权益(节省百分比)'][
0] / item['现用现付'][
0]) * 100))
item['现用现付'] = '${}/小时 or $ {}/月'.format(str(item['现用现付'][0]), str(item['现用现付'][1]))
print(item)
# item的字典在这!
# 清除已经输出的值
for i in table_heads:
del item[i]
# mark2name=
input()
微软虚拟机
最新推荐文章于 2024-11-25 23:21:16 发布
