-1

This is not the same as extract number from a string as beautiful soup returns a beautiful soup object.

I have a local HTML file and I have used BeautifulSoup to Print the text between the pre code tags. This is my current code.

from bs4 import BeautifulSoup

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

s = f.read()

soup = BeautifulSoup(s,"lxml") pre = soup.find_all('pre') print(pre)

This is what gets printed out:

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%
]

How can I return the number (2 in this example) that is before "trades over" on the forth line down and use it as an integer later in my program?

ethertec
  • 11
  • 4

1 Answers1

0

Assuming that is always the fourth line inside the pre tag:

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

Outputs:

2
drec4s
  • 7,946
  • 8
  • 33
  • 54
  • Glad to help. Please consider marking the answer as correct. – drec4s Mar 26 '18 at 00:39
  • sorry I m just a beginner - I thought this worked but it was wrong output. I get error message , line 10, in result = int(pre.split('\n')[3].split('trades over')[0]) AttributeError: 'ResultSet' object has no attribute 'split' – ethertec Mar 26 '18 at 00:54
  • I have also tried just using find instead of find all but get this error line 10, in result = int(pre.split('\n')[3].split('trades over')[0]) TypeError: 'NoneType' object is not callable – ethertec Mar 26 '18 at 00:56
  • See the edited answer. – drec4s Mar 26 '18 at 01:10
  • Still getting an error TypeError: 'NoneType' object is not callable – ethertec Mar 26 '18 at 21:09
  • This is my code - thanks for help from bs4 import BeautifulSoup f = open('/home/trade_result.html', 'r') s = f.read() soup = BeautifulSoup(s,"lxml") pre = soup.find_all('pre') #result = int(pre.split('\n')[3].split('trades over')[0]) old answer result = int(pre[0].split('\n')[3].split('trades over')[0]) – ethertec Mar 26 '18 at 21:11
  • See edited answer, I missed a `.text` call. – drec4s Mar 26 '18 at 23:12
  • thanks drec4s - that definitely works now, much appreciated, iIwill mark it answered . – ethertec Mar 26 '18 at 23:55