Javascript 使用Python执行JS并将结果存储在数组中

Javascript 使用Python执行JS并将结果存储在数组中,javascript,python,html,web-scraping,Javascript,Python,Html,Web Scraping,我正在进行这项工作,其中有一个SVG地图和单选按钮作为过滤器: 为了获得地图中过滤器的结果(国家以蓝色显示),我执行以下javascript片段: var n = $("input[name=adoptionStatus]:checked").val(); (n == undefined || n === "") && (n = "00000000000000000000000000000000");

我正在进行这项工作,其中有一个SVG地图和单选按钮作为过滤器:

为了获得地图中过滤器的结果(国家以蓝色显示),我执行以下javascript片段:

var n = $("input[name=adoptionStatus]:checked").val();
    (n == undefined || n === "") && (n = "00000000000000000000000000000000");
    $(".status-text").hide();
    $("#" + n).show();
    $("#vmap").vectorMap("set", "colors", resetColorsData);
    resetColorsData = {};
    colorsData = {};
    $("#countries_list a").each(function(t, i) {
        $(i).data("adoption-status").indexOf(n) >= 0 && (colorsData[$(i).data("country-code")] = "#2f98cb", resetColorsData[$(i).data("country-codecountry-code")] = "#fefefe")
    });
    $("#vmap").vectorMap("set", "colors", colorsData)
变量
n
用于存储单选按钮的值,如本例中的
cae64c6b731d47cca7565b2a74d11d53

<div class="map-filter-radio radio">
            <label>
                <input type="radio" name="adoptionStatus" alt="IFRS Standards are permitted, but not required, for use by at least some domestic publicly accountable entities, including listed companies and financial institutions." title="IFRS Standards are permitted, but not required, for use by at least some domestic publicly accountable entities, including listed companies and financial institutions." value="cae64c6b731d47cca7565b2a74d11d53">
                IFRS Standards are permitted but not required for domestic public companies
            </label>
</div>


如何在网页上执行JS脚本,并使用
python
获得数组中彩色国家的结果?

通过查看
#countries_list
指定的国家列表,您得到了一个
列表,如下图所示:

<a id="country_bd" data-country-code="bd" data-adoption-status="97f9b22998d546f7856bb1b4f0586521|3adc18f07ff64c908a6d835e08344531|ff784361818644798ea899f81b8b6d61" href="/use-around-the-world/use-of-ifrs-standards-by-jurisdiction/bangladesh/">
    <img src="/-/media/7bfd06a698594c2cb3614578a41caa9e.ashx" alt="Bangladesh">
    Bangladesh
</a>
以下代码列出了所有
输入
标记,并提取了
采用状态
对于其中的每一个,它会提示用户选择一个过滤器(0到4),并通过对
数据采用状态
属性进行过滤来获取所选国家:

import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.ifrs.org/use-around-the-world/use-of-ifrs-standards-by-jurisdiction/")
soup = BeautifulSoup(r.text, "html.parser")

choiceContainer = soup.find("div", {"class":"map-filters"})

choices = [
    (t["title"], t["value"])
    for t in choiceContainer.findAll("input")
]

for idx, choice in enumerate(choices):
    print(f"[{idx}] {choice[0]}")
val = input("Choose a filter index : ") 
choice = choices[int(val)]

print(f"You have chosen {choice[0]}")

selectedAdoptionStatus = choice[1]

countryList = soup.find("div", {"id":"countries_list"})
selectedCountries = [
    {
        "countryCode": t["data-country-code"],
        "adoptionStatus": t["data-adoption-status"].split("|"),
        "link": t["href"],
        "country": t.find("img")["alt"]
    }
    for t in countryList.findAll("a")
    if selectedAdoptionStatus in t["data-adoption-status"].split("|")
]

for it in selectedCountries:
    print(it["country"])


样本输出

[0] IFRS Standards are required for use by all or most domestic publicly accountable entities.
[1] IFRS Standards are permitted, but not required, for use by at least some domestic publicly accountable entities, including listed companies and financial institutions.
[2] IFRS Standards are required or permitted for use by foreign securities issuers.
[3] In most cases an SME may also choose full IFRS Standards. In some cases, an SME may also choose local standards for SMEs.
[4] The body with authority to adopt financial reporting standards is actively studying whether to adopt the <em>IFRS for SMEs</em> Standard.
Choose a filter index : 1
You have chosen IFRS Standards are permitted, but not required, for use by at least some domestic publicly accountable entities, including listed companies and financial institutions.
Bermuda
Cayman Islands
Guatemala
Iraq
Japan
Madagascar
Nicaragua
Panama
Paraguay
Suriname
Switzerland
Timor-Leste
[0]所有或大多数国内公共责任实体都需要使用IFRS标准。
[1] 国际财务报告准则允许但不要求至少一些国内公共责任实体使用,包括上市公司和金融机构。
[2] 外国证券发行人需要或允许使用IFRS标准。
[3] 在大多数情况下,中小企业也可以选择完整的IFRS标准。在某些情况下,中小企业也可以为中小企业选择当地标准。
[4] 有权采用财务报告准则的机构正在积极研究是否采用《中小企业国际财务报告准则》准则。
选择一个筛选器索引:1
您选择的IFRS标准是允许但不是必需的,供至少一些国内公共责任实体使用,包括上市公司和金融机构。
百慕大群岛
开曼群岛
危地马拉
伊拉克
日本
马达加斯加
尼加拉瓜
巴拿马
巴拉圭
苏里南
瑞士
东帝汶

如果(…){…}
而不是那些(糟糕的)
,&&,你为什么不使用可读性更强的
/
..&&一行?@Andreas我从网站上得到了脚本。这不是我的工作
import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.ifrs.org/use-around-the-world/use-of-ifrs-standards-by-jurisdiction/")
soup = BeautifulSoup(r.text, "html.parser")

choiceContainer = soup.find("div", {"class":"map-filters"})

choices = [
    (t["title"], t["value"])
    for t in choiceContainer.findAll("input")
]

for idx, choice in enumerate(choices):
    print(f"[{idx}] {choice[0]}")
val = input("Choose a filter index : ") 
choice = choices[int(val)]

print(f"You have chosen {choice[0]}")

selectedAdoptionStatus = choice[1]

countryList = soup.find("div", {"id":"countries_list"})
selectedCountries = [
    {
        "countryCode": t["data-country-code"],
        "adoptionStatus": t["data-adoption-status"].split("|"),
        "link": t["href"],
        "country": t.find("img")["alt"]
    }
    for t in countryList.findAll("a")
    if selectedAdoptionStatus in t["data-adoption-status"].split("|")
]

for it in selectedCountries:
    print(it["country"])
[0] IFRS Standards are required for use by all or most domestic publicly accountable entities.
[1] IFRS Standards are permitted, but not required, for use by at least some domestic publicly accountable entities, including listed companies and financial institutions.
[2] IFRS Standards are required or permitted for use by foreign securities issuers.
[3] In most cases an SME may also choose full IFRS Standards. In some cases, an SME may also choose local standards for SMEs.
[4] The body with authority to adopt financial reporting standards is actively studying whether to adopt the <em>IFRS for SMEs</em> Standard.
Choose a filter index : 1
You have chosen IFRS Standards are permitted, but not required, for use by at least some domestic publicly accountable entities, including listed companies and financial institutions.
Bermuda
Cayman Islands
Guatemala
Iraq
Japan
Madagascar
Nicaragua
Panama
Paraguay
Suriname
Switzerland
Timor-Leste