Skip to content

Instantly share code, notes, and snippets.

@pvanallen
Last active February 23, 2018 19:44
Show Gist options
  • Save pvanallen/18ef67f967dcc20224ad0539dc7eee2a to your computer and use it in GitHub Desktop.
Save pvanallen/18ef67f967dcc20224ad0539dc7eee2a to your computer and use it in GitHub Desktop.
# these examples assume that the bs4_example.py code has already been run
# https://gist.github.com/pvanallen/0e07e3d24c9885f30d45a2b515a18600#file-bs4_example-py
#
# paste each of these lines, one at a time, into the python command line
# to see the result
soup.title
# 1
# <title>The Dormouse's story</title>
soup.title.string
# 2
# u'The Dormouse's story'
soup.p
# 3
# <p class="title"><b>The Dormouse's story</b></p>
soup.a
# 4
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
soup.find_all('a')
# 5
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
soup.find(id="link3")
# 6
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
pstory = soup.find('p', class_='story')
# 7
print(pstory)
# 8
# <p class="story">Once upon a time there were three little sisters; and their names were
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
# and they lived at the bottom of a well.</p>
pstory.get_text()
# 9
# u'Once upon a time there were three little sisters; and their names were\nElsie,\nLacie and\nTillie;\nand they lived at the bottom of a well.'
print(pstory.get_text())
# 10
# Once upon a time there were three little sisters; and their names were
# Elsie,
# Lacie and
# Tillie;
# and they lived at the bottom of a well.
print(pstory.get_text().replace('\n',' '))
# 11
# Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.
pstory.a['href']
# 12
# u'http://example.com/elsie'
pstory.find_all('a')[1]['href']
# 13
# u'http://example.com/lacie'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment