python - 使用 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
解决方案
如果您想要所有货币,那么只需使用 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']
推荐阅读
- java - 使用 ArrayList 将对象存储在另外 2 个数组中?
- python - 如何在python中使用循环读取一组编号的txt文件?
- python - 如果在数据框 B 中找到数据框 A 的值,Pandas Python 会更新它
- lattice - Lattice Diamond 中的 EBR 块
- javascript - 如何在没有白色闪烁的背景上创建全屏交叉淡入淡出幻灯片?
- docker - Docker 网络仅适用于 --net=host
- apache-spark - 如何使用上述要求严格使用 SPARK SQL 定义 UDF
- automation - 如何安全地设置持续交付?
- owl - 如何在 protege 中导入 XML?
- javascript - Web-Bluetooth:导航器的自定义类型