Install with pip install beautiful soup4
I thought it would be okay to use the parser by default, so I used the default html.parser
instead of lxml
.
import requests
from bs4 import BeautifulSoup
url = input()
html = requests.get(url)
soup = BeautifulSoup(html.content, "html.parser")
Basically this should be fine.
・ Id search (only one can be searched
soup.find (id =" id name ")
・ Css selector search (only one can be searched
ʻIng.select_one ("css selector name") `
When finding all the elements that match your search
If id
find_all (id name)
with css selector
select (.class attribute name)
See also: [Differences in how to use find_all () and select () in Beautiful Soup]
(https://gammasoft.jp/blog/difference-find-and-select-in-beautiful-soup-of-python/)
When searching for things like <h3 class =" A B ">
(having multiple class attributes) with select, use select_one (.A.B)
.
Recommended Posts