Getting Started
After you installed this package, the next step is to import the package into your code and start using the functions.
from botcity.utils.parser import StringParser
As a demonstration of the library, let's build a simple
example together that will use the StringParser
to read the content between the paragraph tags of the following HTML snippet:
<h1>Example</h1>
<p>A paragraph with some content.
This is a test of multiple
lines for the parser.</p>
Instantiating the Parser
As a demonstration we will assume that the HTML snippet above is stored into the
text
variable.
from botcity.utils.parser import StringParser
parser = StringParser(text)
Extracting Text
Looking into the snippet we know that the desired data is contained between the tags <p>
and </p>
.
Based on that we can use the goto_tag_start
from the StringParser
object and the read_next_lines_until
to fetch the data as a list of lines.
Here is how we do it:
# Move cursor to end of <p> tag
parser.goto_tag_end("<p>")
# Read all lines until </p> is found
paragraph = parser.read_next_lines_until("</p>")
Complete code
Let's take a look into the complete code:
from botcity.utils.parser import StringParser
parser = StringParser(text)
# Move cursor to end of <p> tag
parser.goto_tag_end("<p>")
# Read all lines until </p> is found
paragraph = parser.read_next_lines_until("</p>")
Pro Tip
This plugin allow you to use method chaining
so the code above could be written as:
from botcity.utils.parser import StringParser
paragraph = StringParser(text).goto_tag_end("<p>").read_next_lines_until("</p>")
Next Steps
Check our examples and experiment with the API. Let us know where it can be improved.
Have fun automating!