Categories
Python Answers

How to parse HTML using Python?

Sometimes, we want to parse HTML using Python.

In this article, we’ll look at how to parse HTML using Python.

How to parse HTML using Python?

To parse HTML using Python, we can use BeautfulSoup.

We install it by running:

pip install beautifulsoup

Then we can write:

from bs4 import BeautifulSoup
html_doc = """<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.prettify())

to add an HTML string and parse it with the BeautifulSoup class.

Then we can print the parsed document in the last line.

We can get the links from the HTML string with the find_all method:

from bs4 import BeautifulSoup
html_doc = """<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
for link in soup.find_all('a'):
    print(link.get('href'))

We just pass in the selector for the elements we wan to get.

Also, we can get all the text from the page with get_text():

from bs4 import BeautifulSoup
html_doc = """<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.get_text())

Conclusion

To parse HTML using Python, we can use BeautfulSoup.

Categories
Python Answers

How to pretty printing XML in Python?

Sometimes, we want to pretty print XML in Python.

In this article, we’ll look at how to pretty print XML in Python.

How to pretty printing XML in Python?

To pretty print XML in Python, we can use the xml.dom.minidom.parseString method.

For instance, we write:

import xml.dom.minidom

xml_string = '''
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
'''
dom = xml.dom.minidom.parseString(xml_string)
pretty_xml_as_string = dom.toprettyxml()
print(pretty_xml_as_string)

We call xml.dom.minidom.parseString with xml_string to create the dom object from the XML string.

Then we call dom.toprettyxml to return a prettified version of the XML string.

Therefore, we see:

<?xml version="1.0" ?>
<note>


    <to>Tove</to>


    <from>Jani</from>


    <heading>Reminder</heading>


    <body>Don't forget me this weekend!</body>


</note>

printed.

We can also call xml.dom.minidom.parse with the XML file path and do the same thing.

For instance, we write:

file.xml

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

main.py

import xml.dom.minidom

dom = xml.dom.minidom.parse('file.xml')
pretty_xml_as_string = dom.toprettyxml()
print(pretty_xml_as_string)

and get the same result.

The only difference is that we pass in the file path to xml.dom.minidom.parse.

Conclusion

To pretty print XML in Python, we can use the xml.dom.minidom.parseString method.

We can also call xml.dom.minidom.parse with the XML file path and do the same thing.

Categories
Python Answers

How to create a long multi-line string with Python?

Sometimes, we want to create a long multi-line string with Python.

In this article, we’ll look at how to create a long multi-line string with Python.

How to create a long multi-line string with Python?

To create a long multi-line string with Python, we can enclose our string content with ''' or """.

For instance, we write:

s = """ This is a very
        long string if I had the
        energy to type more and more ..."""

or:

s = ''' This is a very
        long string if I had the
        energy to type more and more ...'''

to define the s multi-line string.

Conclusion

To create a long multi-line string with Python, we can enclose our string content with ''' or """.

Categories
Python Answers

How to capture SIGINT in Python?

Sometimes, we want to capture SIGINT in Python.

In this article, we’ll look at how to capture SIGINT in Python.

How to capture SIGINT in Python?

To capture SIGINT in Python, we can call the signal.signal method.

For instance, we write:

import signal
import sys

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!')
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
signal.pause()

We have the signal_handler function that runs when the SIGINT signal is emitted.

We call signal.signal with signal.SIGINT to listen for the SIGINT signal and run signal_handler when it’s emitted.

Then we call signal.pause to pause the script and let us watch for the signal.

Now when we press Ctrl+C, we should see 'You pressed Ctrl+C!' printed.

Conclusion

To capture SIGINT in Python, we can call the signal.signal method.

Categories
Python Answers

How to call a function within class with Python?

Sometimes, we want to call a function within class with Python.

In this article, we’ll look at how to call a function within class with Python.

How to call a function within class with Python?

To call a function within class with Python, we call the function with self before it.

For instance, we write:

class Coordinates:
    def distToPoint(self, p):
        """
        Use pythagoras to find distance
        (a^2 = b^2 + c^2)
        """

    def isNear(self, p):
        self.distToPoint(self, p)

We call the distToPoint instance method within the Coordinates class by calling self.distToPoint.

self is variable storing the current Coordinates class instance.

Conclusion

To call a function within class with Python, we call the function with self before it.