首页 > 解决方案 > 使用 BeautifulSoup - Python 从下拉列表中提取选项值

问题描述

如何从以下链接的下拉列表中提取选项(例如澳元):https://transferwise.com/us]

我试过这个

import requests
from bs4 import *
twise__url = 'https://transferwise.com/us'
page = requests.get(twise__url)
        soup = BeautifulSoup(page.content, 'html.parser')
        title = soup.title.text # gets you the text of the <title>(...)</title>
        for itemText in soup.find_all('button', attrs={'class':'btn btn-input btn-input-inverse btn-addon btn-lg dropdown-toggle'}):
            print(itemText))

在此处输入图像描述

预期输出:

NZD
EUR
GBP
IND
CZK
MYR
CAD

etc

标签: pythonhtmlweb-scrapingbeautifulsoup

解决方案


如果您想要所有货币,那么只需使用 API。

例如,使用recent端点:

import requests

headers = {
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}
currencies = requests.get("https://transferwise.com/gateway/v1/savings/recent", headers=headers).json()

for currency in currencies:
    print(f"{currency['sourceCurrency']} - {currency['targetCurrency']}:")
    print(f"{currency['sendAmount']} -> {currency['savings']}")

这打印:

EUR - UAH:
58.43 -> 10.13
EUR - INR:
227.39 -> 22.18
EUR - LKR:
18.31 -> 9.16
AUD - GBP:
127.0 -> 12.69
GBP - INR:
20.32 -> 6.8
GBP - EUR:
21.66 -> 5.53
CAD - EUR:
18.66 -> 11.22
PLN - UAH:
88.2 -> 36.53
USD - INR:
19.49 -> 11.75
...

给定对history的最后几天还有一个端点。30

例如:

from datetime import datetime

import requests

headers = {
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}
history = requests.get("https://transferwise.com/rates/history?source=AUD&target=EUR&length=30", headers=headers).json()
for item in history:
    print(f"{datetime.fromtimestamp(item['time'] / 1000)}")
    print(f"{item['source']} -> {item['target']}: {item['value']}")

输出:

2020-10-14 02:00:00
AUD -> EUR: 0.6073
2020-10-15 02:00:00
AUD -> EUR: 0.603704
2020-10-16 02:00:00
AUD -> EUR: 0.604669
2020-10-17 02:00:00
AUD -> EUR: 0.603994
2020-10-18 02:00:00
AUD -> EUR: 0.603979
2020-10-19 02:00:00
AUD -> EUR: 0.602059
2020-10-20 02:00:00
AUD -> EUR: 0.596948
2020-10-21 02:00:00
...

但是,如果您想要所有货币短名称,请尝试以下操作:

import requests

headers = {
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}
currencies = requests.get("https://transferwise.com/gateway/v1/savings/recent", headers=headers).json()

output = []
for currency in currencies:
    output.extend([currency['sourceCurrency'], currency['targetCurrency']])

print(sorted(list(set(output))))

输出:

['AUD', 'BDT', 'CNY', 'CZK', 'EUR', 'GBP', 'HRK', 'HUF', 'IDR', 'INR', 'JPY', 'KES', 'LKR', 'MYR', 'NGN', 'NOK', 'NZD', 'PHP', 'PKR', 'PLN', 'RON', 'RUB', 'SEK', 'SGD', 'TRY', 'UAH', 'USD', 'VND']

推荐阅读