Searched for a specific item:

game_descriptions = html.cssselect('#game_area_description')[0] 

As a result, I received such a block:

 <div id="game_area_description" class="game_area_description"> <strong>Бамая популярная ΠΈΠ³Ρ€Π° Π² Steam</strong> <br>Π•ΠΆΠ΅Π΄Π½Π΅Π²Π½ΠΎ ΠΌΠΈΠ»Π»ΠΈΠΎΠ½Ρ‹ ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² ΠΏΠΎ всСму ΠΌΠΈΡ€Ρƒ Π²ΡΡ‚ΡƒΠΏΠ°ΡŽΡ‚ Π² Π±ΠΈΡ‚Π²Ρƒ ΠΎΡ‚ Π»ΠΈΡ†Π° ΠΎΠ΄Π½ΠΎΠ³ΠΎ..... 

How to get all the text with html attributes?

.text prints blank lines

Update:

 import lxml.html def get_html(request): return lxml.html.fromstring(request.text) html = get_html(r) game_descriptions = html.cssselect('#game_area_description')[0] 
  • And what you got is not suitable? there are both attributes and html - gil9red
  • need to get rid of the div id = "game_area_description" to save the rest of the database - danilshik
  • Interesting .. but try: ''.join([html.tostring(child) for child in game_descriptions.iterchildren()]) - gil9red
  • 'HtmlElement' object has no attribute 'tostring' - danilshik
  • do you have html who? from lxml import html ? Add a minimal reproducible example to the question so as not to play riddles - gil9red

1 answer 1

Try:

 import lxml.html def to_string(node): return lxml.html.tostring(node, encoding='unicode') text = """<div id="game_area_description" class="game_area_description"> <strong>Бамая популярная ΠΈΠ³Ρ€Π° Π² Steam</strong> <br>Π•ΠΆΠ΅Π΄Π½Π΅Π²Π½ΠΎ ΠΌΠΈΠ»Π»ΠΈΠΎΠ½Ρ‹ ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² ΠΏΠΎ всСму ΠΌΠΈΡ€Ρƒ Π²ΡΡ‚ΡƒΠΏΠ°ΡŽΡ‚ Π² Π±ΠΈΡ‚Π²Ρƒ ΠΎΡ‚ Π»ΠΈΡ†Π° ΠΎΠ΄Π½ΠΎΠ³ΠΎ.....""" root = lxml.html.fromstring(text) game_descriptions = root.cssselect('#game_area_description')[0] print(''.join(to_string(child) for child in game_descriptions.iterchildren())) 

Console:

 <strong>Бамая популярная ΠΈΠ³Ρ€Π° Π² Steam</strong> <br>Π•ΠΆΠ΅Π΄Π½Π΅Π²Π½ΠΎ ΠΌΠΈΠ»Π»ΠΈΠΎΠ½Ρ‹ ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² ΠΏΠΎ всСму ΠΌΠΈΡ€Ρƒ Π²ΡΡ‚ΡƒΠΏΠ°ΡŽΡ‚ Π² Π±ΠΈΡ‚Π²Ρƒ ΠΎΡ‚ Π»ΠΈΡ†Π° ΠΎΠ΄Π½ΠΎΠ³ΠΎ..... 
  • changed the name of the variable so that there is no confusion. as node get elements from iterchildren - gil9red
  • Thanks, earned. I just initially wanted to use text in a mobile application, but I thought about how to keep similar markup, so I decided to save it with html tags - danilshik
  • @danilshik If it helped, you know what to do :) - gil9red