M
M
mironi2021-03-10 07:54:16
Python
mironi, 2021-03-10 07:54:16

How to parse strings in Python?

Good time!
Tell me how to parse the desired text from strings?
Help a newbie, he broke his head!
Given:
604850325d4b7559461384.png

<tr>
<td>
<br/><br/>
<i class="icon icon-user" data-selector=".icon" title="Автор"></i>   Барышева Олеся<br/>
<i class="icon icon-support" data-selector=".icon" title="E-mail"></i>   [email protected]<br/>
<i class="icon icon-phone" data-selector=".icon" title="Телефон"></i>   89188565504<br/>
</td>
</tr>


it is required to parse lines 1,2,3
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
datka, 2021-03-10
@mironi

soup = BeautifulSoup(html)
author_text = soup.find('i',{'class':'icon icon-user'})
email_text = soup.find('i',{'class':'icon icon-support'})
phone_text = soup.find('i',{'class':'icon icon-phone'})

print(author_text.next)
print(email_text.next)
print(phone_text.next)

Conclusion:
Пыльнев  Анатолий
[email protected]
89055663563

Old Answer
from bs4 import BeautifulSoup
html="""
<tr>
<td>
<br/><br/>
<i class="icon icon-user" data-selector=".icon" title="Автор"></i>   Барышева Олеся<br/>
<i class="icon icon-support" data-selector=".icon" title="E-mail"></i>   [email protected]<br/>
<i class="icon icon-phone" data-selector=".icon" title="Телефон"></i>   89188565504<br/>
</td>
</tr>
"""
soup = BeautifulSoup(html)
my_text = soup.find('td')
print(my_text.get_text().split())

Вывод.
['Барышева', 'Олеся', '[email protected]', '89188565504']

Либо без .split(), смотря как вы хотите получать/хранить данные.
Барышева Олеся
[email protected]
89188565504

UPD: Если у вас будет больше одного человека в коде можно сделать так.
from bs4 import BeautifulSoup

html="""
<tr>
<td>
<br/><br/>
<i class="icon icon-user" data-selector=".icon" title="Автор"></i>   Барышева Олеся<br/>
<i class="icon icon-support" data-selector=".icon" title="E-mail"></i>   [email protected]<br/>
<i class="icon icon-phone" data-selector=".icon" title="Телефон"></i>   89188565504<br/>
</td>
</tr>

<tr>
<td>
<br/><br/>
<i class="icon icon-user" data-selector=".icon" title="Автор"></i>   Иван Иванович<br/>
<i class="icon icon-support" data-selector=".icon" title="E-mail"></i>   [email protected]<br/>
<i class="icon icon-phone" data-selector=".icon" title="Телефон"></i>   232321113312<br/>
</td>
</tr>

<tr>
<td>
<br/><br/>
<i class="icon icon-user" data-selector=".icon" title="Автор"></i>   Темный лорд<br/>
<i class="icon icon-support" data-selector=".icon" title="E-mail"></i>   [email protected]<br/>
<i class="icon icon-phone" data-selector=".icon" title="Телефон"></i>   80002111122<br/>
</td>
</tr>
"""

soup = BeautifulSoup(html)
my_text = soup.findAll('td')
for text in my_text:
    print(text.get_text().split())

Вывод будет таков
['Барышева', 'Олеся', '[email protected]', '89188565504']
['Иван', 'Иванович', '[email protected]', '232321113312']
['Темный', 'лорд', '[email protected]', '80002111122']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question