Skip to content

Instantly share code, notes, and snippets.

@ysawa
Created February 27, 2017 04:53
Show Gist options
  • Save ysawa/8724832e11299d602d56e15e9bc2bdf2 to your computer and use it in GitHub Desktop.
Save ysawa/8724832e11299d602d56e15e9bc2bdf2 to your computer and use it in GitHub Desktop.
from bs4 import BeautifulSoup
html = """
<html>
<head></head>
<body>
<div id="todo">
<h1>Todo list</h1>
<ul class="items">
<li>hello world</li>
<li>hoge fuga</li>
<li>foo bar</li>
</ul>
</div>
"""
soup = BeautifulSoup(html, 'html.parser')
h1 = soup.select_one("div#todo > h1").string
print("h1 " + h1)
# Todo list
li_list = soup.select("div#todo li")
for li in li_list:
print(li.string)
# hello world
# hoge fuga
# foo bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment