M
M
MichaelMih2021-06-23 22:47:49
Python
MichaelMih, 2021-06-23 22:47:49

How to search in python text and get some value from that text?

Input:
file: (

<div class="form-item">
                    <label for="firstname">Введите фамилию*</label>
                    <input type="text" name="firstname" id="firstname" required/>
                </div>
                <div class="form-item">
                    <label for="lastname">Введите имя*</label>
                    <input type="text" name="lastname" id="lastname" required />
                </div>
                <div class="form-item">
                    <label for="patronymic">Введите Отчество</label>
                    <input type="text" name="patronymic" id="patronymic"/>
                </div>
)
String: 'firstname'
Expected result: "Enter last name*"
How can I do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2021-06-24
@MichaelMih

Read about Beautiful Soup, maybe you need to dig in this direction or use something similar

D
Dmitry, 2021-06-24
@LazyTalent

Use lxml

>>> import lxml.html as lh
>>> 
>>> raw_html = """<div class="form-item">
...                     <label for="firstname">Введите фамилию*</label>
...                     <input type="text" name="firstname" id="firstname" required/>
...                 </div>
...                 <div class="form-item">
...                     <label for="lastname">Введите имя*</label>
...                     <input type="text" name="lastname" id="lastname" required />
...                 </div>
...                 <div class="form-item">
...                     <label for="patronymic">Введите Отчество</label>
...                     <input type="text" name="patronymic" id="patronymic"/>
...                 </div>
... """
>>> 
>>> html = lh.fromstring(raw_html)
>>> html.xpath('.//label[@for="firstname"]/text()')[0]
'Введите фамилию*'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question