首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用BeautifulSoup - Python从下拉列表中提取选项值

使用BeautifulSoup - Python从下拉列表中提取选项值
EN

Stack Overflow用户
提问于 2020-11-14 04:37:46
回答 1查看 49关注 0票数 0

如何从以下链接的下拉列表中提取选项(eg.AUD):https://transferwise.com/us](https://transferwise.com/us%5D)

我试过了

代码语言:javascript
复制
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))

预期输出:

代码语言:javascript
复制
NZD
EUR
GBP
IND
CZK
MYR
CAD

etc
EN

回答 1

Stack Overflow用户

发布于 2020-11-14 05:06:44

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

例如,使用recent端点:

代码语言:javascript
复制
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']}")

这将打印:

代码语言:javascript
复制
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
...

对于给定的配对,还有一个用于最近30天的history端点。

例如:

代码语言:javascript
复制
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']}")

输出:

代码语言:javascript
复制
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
...

但是,如果您只想要所有的货币缩写名称,请尝试这样做:

代码语言:javascript
复制
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))))

输出:

代码语言:javascript
复制
['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']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64827736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档