Package: htmls
htmlsUsageRequirementsHtmls requireslxml <http://lxml.de/>. lxml requires libxml2 and libxslt,
so you have to install those libraries. You do not need to install lxml,
htmls will pull it in as a dependency.Tested with Python 3.8 and 3.10.Tip for Mac OSXHow to install libxml2 and libxslt is described in their docs, but their
docs does not mention that libxml2 and libxslt is available in Homebrew
for OSX. You can install them with::$ brew install libxml2 libxsltInstallWhen you have install libxml2 and libxslt, you can install htmls with::$ pip install htmlsUsage exampleimportunittestimporthtmlsclassExampleTest(unittest.TestCase):deftest_example(self):selector=htmls.S("""<html><body><a href="#hello">Hello</a><a href="#cruel" class="btn btn-default btn-lg">CruelWorld</a></body></html>""")# Various ways of finding elementsself.assertEqual(selector.count('a'),2)button=selector.one('a.btn')# Single htmls.Elementwithself.assertRaises(htmls.NotExactlyOneMatchError):button=selector.one('a')# Fails because the a selector matches 2 elements.buttons=selector.list('a.btn')# List of htmls.Element# Debuggingprintselector.prettify()# Or selector.prettyprint()printbutton.prettify()# Or button.prettyprint()# Find the text of an element with normalized whitespaceself.assertEqual(selector.one('a.btn').text_normalized,'Cruel World')# Working with CSS classesself.assertTrue(selector.one('a.btn').hasclass('btn-default'))self.assertTrue(selector.one('a.btn').hasclasses(['btn-default','btn-lg']))self.assertFalse(selector.one('a.btn').hasclasses(['btn-default','btn-lg','invalidclass']))# Working with attributesself.assertEqual(selector.one('a.btn')['href'],'#cruel')self.assertTrue(selector.one('a.btn').hasattribute('href'),'#cruel')The example above is also available intests/test_example.py.DevelopUse conventional commits for GIT commit messagesSeehttps://www.conventionalcommits.org/en/v1.0.0/.
You can use this git commit message format in many different ways, but the easiest is:Use commitizen:https://commitizen-tools.github.io/commitizen/commit/Use an editor extension, likehttps://marketplace.visualstudio.com/items?itemName=vivaxy.vscode-conventional-commitsfor VScode.Just learn to write the format by hand (can be error prone to begin with, but it is fairly easy to learn).Install hatch and commitizenNOTE: You only need hatch if you need to build releases, and you
only need commitizen for releases OR to make it easy to follow
conventional commits for your commit messages
(seeUse conventional commits for GIT commit messagesabove).First install pipx with:$ brew install pipx
$ pipx ensurepathThen install hatch and commitizen:$ pipx install hatch 
$ pipx install commitizenSeehttps://github.com/pypa/pipx,https://hatch.pypa.io/latest/install/andhttps://commitizen-tools.github.io/commitizen/for more install alternatives if
needed, but we really recommend using pipx since that is isolated.Install development dependenciesInstall a local python version with pyenv:$ pyenv install 3.10
$ pyenv local 3.10Create virtualenv:$ ./recreate-virtualenv.shAlternatively, create virtualenv manually (this does the same as recreate-virtualenv.sh):$ python -m venv .venvInstall dependencies in a virtualenv:$ .venv/bin/pip install -v ".[dev,test]"Tests$ pytestDocumentationWe have no HTML API docs at this time. This is a very small library,
it is less than 200 lines of well documented code. So just readhtmls.py,
or usepydoc htmlsafter you have installed the library.Also check out the code intests/test_htmls.py. We have 100% test coverage,
and the tests are good examples.How to release htmlsRelease (create changelog, increment version, commit and tag the change) with:$ cz bump
$ git push && git push --tagsNOTE:cz bumponly works if conventional commits (see section about that above) is used.cz bumpcan take a specific version etc, but it automatically select the correct version
if conventional commits has been used correctly. Seehttps://commitizen-tools.github.io/commitizen/.Theczcommand comes fromcommitizen(install documented above).Release to pypi:$ hatch build -t sdist
$ hatch publish
$ rm dist/*              # optional cleanup