Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add 2nd example to chapter2
  • Loading branch information
will-i-amv authored Jun 30, 2021
commit 06425480fd298701066b9bfc560e19bff944aa31
22 changes: 22 additions & 0 deletions v2/chapter2/2-selectByOtherArguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from urllib.request import urlopen
from bs4 import BeautifulSoup


html = urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
bs = BeautifulSoup(html, "html.parser")

# Select by tags
titles = bs.find_all(['h1', 'h2','h3','h4','h5','h6'])
print([title for title in titles])

# Select by tag attributes
allText = bs.find_all('span', {'class':{'green', 'red'}})
print([text for text in allText])

# Select by text content of tags
nameList = bs.find_all(text='the prince')
print(len(nameList))

# Select by tags that contains a particular attribute
titles = bs.find_all(id='title', class_='text')
print([title for title in titles])