Python Forum
find chars after chars in 'pre' tag
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find chars after chars in 'pre' tag
#3
You never call text,if you do text inside pre tag will get a \n for each line.
Then can split on \n and take out line you want.
from bs4 import BeautifulSoup

html = '''\
<html>
<body>

<pre>
Text in a pre element
is displayed in a fixed-width
XYZ57 ABCD 141800
font, and it preserves
both spaces and
line breaks
</pre>

</body>
</html>'''

soup = BeautifulSoup(html,'lxml')
y = soup.find('pre')
# Call text
text = y.text
my_line = ''
for line in text.split('\n'):
    if line.startswith('XYZ'):
    my_line += line

print(my_line)
Output:
XYZ57 ABCD 141800
Reply


Messages In This Thread
find chars after chars in 'pre' tag - by Fran_3 - Aug-17-2017, 11:18 PM
RE: find chars after chars in 'pre' tag - by snippsat - Aug-18-2017, 12:44 AM
RE: find chars after chars in 'pre' tag - by Fran_3 - Aug-19-2017, 04:27 PM
RE: find chars after chars in 'pre' tag - by Fran_3 - Aug-19-2017, 08:30 PM
RE: find chars after chars in 'pre' tag - by Fran_3 - Aug-20-2017, 02:56 PM
RE: find chars after chars in 'pre' tag - by Fran_3 - Aug-22-2017, 11:44 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020