Search code examples
pythonloopsweb-scrapinghttprequest

Print all id numbers from class in http-request


I would like to have a ready-made list containing id numbers.

<dl class="welcome" id="fdgvs_4455256765">
<dl class="welcome" id="34wfdsf_235250861">
<dl class="welcome" id="v3dr234fz_1242566862">

I already have a piece of code, but I miss the cut from the same id numbers.

import requests
from requests_html import HTMLSession

...

links = response.html.find(".welcome")
for one in links:
    divone = response.html.find('.id')
    print(one)

Output:

<Element 'dl' class=('welcome',) id='fdgvs_4455256765'>
<Element 'dl' class=('welcome',) id='34wfdsf_235250861'>
<Element 'dl' class=('welcome',) id='v3dr234fz_1242566862'>

What i want?:

4455256765
235250861
1242566862

Thanks


Solution

  • You split the string by id=, then you split the second part by _ then you print the result by removing '> at the end.

    str = "<Element 'dl' class=('welcome',) id='v3dr234fz_1242566862'>"
    
    print(str.split("id=")[1].split("_")[1][:-2])