Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用BeautifulSoup-Python从下拉列表中提取选项值_Python_Html_Web Scraping_Beautifulsoup - Fatal编程技术网

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

使用BeautifulSoup-Python从下拉列表中提取选项值,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,如何从以下链接的下拉列表中提取选项(例如AUD): 我试过这个 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

如何从以下链接的下拉列表中提取选项(例如AUD):

我试过这个

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))

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

例如,使用
最近的
端点:

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
...
对于给定的一对,在过去的
30天内也有
history
端点

例如:

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
...
['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']
但是,如果您只需要所有货币短名称,请尝试以下操作:

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))))
输出:

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
...
['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']

到目前为止你试过什么?请提供我刚刚更新的问题您是否只想要值
AUD
?还是所有关于转移的信息?你到底想删除什么?我想删除所有的值,例如AUD,gbp。我已经更新了我的答案,@itgeek。如果你发现我的答案有用,考虑投票和/或接受它。