How to use BS4 find_all (key)
from bs4 import BeautifulSoup
import re
html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1"><! -- E lsie --></a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie< /a> and <a href="http://example.com/tillie" class="sister" id="link3">Tilli e</a>; and they lived at the bottom of a well.</p> <p class="story">... </p> """
soup = BeautifulSoup(html, "lxml") # "LXML parsing"
print soup.find("p") Find the first p tag
print soup.find_all("p") # Find all p tags and return them as a list
print soup.find_all("p") [2] # Find all p tags and return them as a list
for tag in soup.find_all(re.compile("^b")) :Match with a regular
print tag.name
""" ... ... """
print soup.find_all(["title"."b"]) Search by list, return data as long as there is one match
"""
[<title>The Dormouse's story</title>, <b>The Dormouse's story</b>]
"""
print soup.find_all(id="link2")
"""
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie<\n/a> and\n</a>]
"""
print(soup.find_all(class_="sister")) # class uses class_ specifically
""" [<a class="sister" href="http://example.com/elsie" id="link1"><! -- E\nlsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie<\n/a> and\n</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tilli\ne</a>] """
print(soup.find_all(text="The"))
print(soup.find_all(text="The Dormouse's story"))
[] [u"The Dormouse's story", u"The Dormouse's story"] ""
print(soup.find_all("a",class_="sister")) The class attribute of the a tag is sister
Copy the code
How to use BS4 select
from bs4 import BeautifulSoup
import re
html = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1"><! -- E lsie --></a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie< /a> and <a href="http://example.com/tillie" class="sister" id="link3">Tilli e</a>; and they lived at the bottom of a well.</p> <p class="story">... </p> """
soup = BeautifulSoup(html, "lxml") # "LXML parsing"
print soup.select("title") Search by tag name
print soup.select("a") Locate multiple tags at a time
print soup.select(".sister") Select * from class
print soup.select("#link1") Search by id
print soup.select("p #link1") Look up the id inside the # p tag
"""
[<a class="sister" href="http://example.com/elsie" id="link1"><!-- E\nlsie --></a>]
"""
print soup.select("head > title") # Offspring (hierarchy)
print soup.select("a[class="sister"]") # tag a class=sister element
""" [<a class="sister" href="http://example.com/elsie" id="link1"><! -- E\nlsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie<\n/a> and\n</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tilli\ne</a>] """
print(soup.select("title") [0].get_text()) Get the content between tags
"""
The Dormouse's story
"""
Copy the code