使用Beautiful Soup如何返回此值并将其用作整数?

时间:2018-03-26 00:26:12

标签: python beautifulsoup

这与字符串中的提取数字不同,因为美丽的汤会返回一个美丽的汤对象。

我有一个本地HTML文件,我使用BeautifulSoup在预编码标签之间打印文本。这是我目前的代码。

来自bs4 import BeautifulSoup

f = open(' /home/stats/trade_result.html',' r')

s = f.read()

汤= BeautifulSoup(s," lxml") pre = soup.find_all(' pre') 打印(预)

这是打印出来的:

last balance: xxxxxx (-0.07%)
buy hold: xxxxxx (-0.08%)
vs. buy hold: 0.01%
2 trades over 8 days (avg 0.25 trades/day)
win/loss: 0/1
error rate: 100.00%
]

如何返回"交易之前的数字(本例中为2)"在第四行,并在我的程序中稍后将其用作整数?

1 个答案:

答案 0 :(得分:0)

假设始终是pre标记内的第四行:

result = int(pre[0].text.split('\n')[3].split('trades over')[0])

输出:

2