Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
spliting html code with br tag
#2
To do a test,also see that this code can be run then it easier for people to help.
from bs4 import BeautifulSoup
import re

html = '''\
<span class="annonce_get_description" itemprop="description">
Smartphones<br>
<b>Double puces</b>
<br>
Mémoire : 64 GO
<br>
Bluetooth Wifi <b>4G</b>
<br>
Ecran 5.8 pouces
<br>
Appareil photo 12 MP
<br>
Bon état
<br>
<span class="annonce_description_preview "> </span></span>'''

soup = BeautifulSoup(html, 'lxml')
Use:
>>> print(tags.text)
tags = soup.find(class_="annonce_get_description")

Smartphones
Double puces

Mémoire : 64 GO

Bluetooth Wifi 4G

Ecran 5.8 pouces

Appareil photo 12 MP

Bon état

 
>>> print(repr(tags.text.strip()))
'Smartphones\nDouble puces\n\nMémoire : 64 GO\n\nBluetooth Wifi 4G\n\nEcran 5.8 pouces\n\nAppareil photo 12 MP\n\nBon état'
With .text get all br tags,see when use repr() that if split on \n\n it should keep the structure.
>>> br_tags = tags.text.strip().split('\n\n')
>>> br_tags
['Smartphones\nDouble puces',
 'Mémoire : 64 GO',
 'Bluetooth Wifi 4G',
 'Ecran 5.8 pouces',
 'Appareil photo 12 MP',
 'Bon état']

>>> print(br_tags[0])
Smartphones
Double puces

>>> print(br_tags[2])
Bluetooth Wifi 4G
Reply


Messages In This Thread
spliting html code with br tag - by yokaso - Aug-04-2019, 07:04 AM
RE: spliting html code with br tag - by snippsat - Aug-04-2019, 09:36 AM
RE: spliting html code with br tag - by yokaso - Aug-05-2019, 07:14 AM
RE: spliting html code with br tag - by snippsat - Aug-05-2019, 09:41 AM
RE: spliting html code with br tag - by yokaso - Aug-06-2019, 08:01 AM
RE: spliting html code with br tag - by snippsat - Aug-06-2019, 10:58 AM
RE: spliting html code with br tag - by yokaso - Aug-06-2019, 11:49 AM
RE: spliting html code with br tag - by snippsat - Aug-06-2019, 12:15 PM
RE: spliting html code with br tag - by yokaso - Aug-06-2019, 12:39 PM
RE: spliting html code with br tag - by snippsat - Aug-06-2019, 02:35 PM
RE: spliting html code with br tag - by yokaso - Aug-07-2019, 07:23 AM
RE: spliting html code with br tag - by snippsat - Aug-07-2019, 03:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Populating list items to html code and create individualized html code files ChainyDaisy 0 1,608 Sep-21-2022, 07:18 PM
Last Post: ChainyDaisy
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,686 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,393 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  How to get the href value of a specific word in the html code julio2000 2 3,242 Mar-05-2020, 07:50 PM
Last Post: julio2000
  Embedding HTML Code in Python kendias 5 4,317 Jan-27-2019, 01:43 AM
Last Post: kendias
  Help with Python and HTML code karlo_ds 4 3,475 Oct-16-2017, 03:03 PM
Last Post: karlo_ds

Forum Jump:

User Panel Messages

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