Home > Software engineering >  Python-Requests How do I capture a specific section of html code from response?
Python-Requests How do I capture a specific section of html code from response?

Time:09-10

how do I request a specific tag? For example, I want to get the footer as an output instead of the whole HTML page. how do I do that?

import requests as req
    
resp = req.get("site")    
print(resp.text)

I want to get only this as the output instead of the whole HTML file; is it possible?

<footer >
    <ol>
        <li >
            <a  aria-current="page" href="index.html">Home</a>
        </li>
        <li >
            <a  href="about_us.html"> About us</a>
        </li>
        <li >
            <a  href="ticket.html"> Submit a ticket</a>
        </li>
        <li >
            <a  href="tos.html"> Terms of use</a>
        </li>
        <li >
            <a  href="donate.html"> Donate</a>
        </li>
        <li >
            <a  href="news.html"> News</a>
        </li>
        <li >
            <a  href="quotes.html"> Quotes</a>
        </li>
    </ol>
</footer>

CodePudding user response:

You can use (requests-HTML) instead of requests. Here you can extract specific classes from a html page.

This should work:

from requests_html import HTMLSession

session = HTMLSession()

r = session.get('http://ilyabr.com')
print( r.html.find('.footer', first=True).html )
  • Related