int()的文本无效不受支持的操作数类型

2024-09-30 18:31:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习Python,但在这一刻陷入了困境:

from tkinter import *
import requests
from bs4 import BeautifulSoup


r = requests.get('https://www.nbg.gov.ge/index.php?m=582')
c = r.content
soup = BeautifulSoup (c, 'html.parser')

headers = soup.find('div', {'id': 'currency_id'})
table = headers.find('table')
listCurrency = table.find_all("td")
usdCurrency = listCurrency[-13].text.strip()
usdCurrency.replace(',', '.')
usdCurrency = int(usdCurrency)
result = int(input("Input Number")
result = result/usdCurrency

我得到了这个错误:

invalid literal for int() with base 10: '3.2896'
    usdCurrency = int(usdCurrency)
TypeError: unsupported operand type(s) for /: 'int' and 'str'

Tags: fromimportidfortableresultfindrequests
1条回答
网友
1楼 · 发布于 2024-09-30 18:31:32

出现这些错误的原因是,usdCurrency的值是小数(例如3.2896),并且您无法使用

usdCurrency = int(usdCurrency)

这些代码行导致了错误,您应该使用它们来避免错误

usdCurrency = int(float(usdCurrency))

一切都完成了

from tkinter import *
import requests
from bs4 import BeautifulSoup


r = requests.get('https://www.nbg.gov.ge/index.php?m=582')
c = r.content
soup = BeautifulSoup (c, 'html.parser')

headers = soup.find('div', {'id': 'currency_id'})
table = headers.find('table')
listCurrency = table.find_all("td")
usdCurrency = listCurrency[-13].text.strip()
usdCurrency.replace(',', '.')
usdCurrency = int(float(usdCurrency))
result = int(input("Input Number"))
result = result/usdCurrency

谢谢

相关问题 更多 >