# 设定网址
url = "https://www.xyblog.cc/"
# 获取网页html
r = req.get(url)
# 导入 html 进入 beautifulsoup4
soup = BeautifulSoup(r.text, features="html.parser")
# 获取所有的元素
for ele in soup.find_all():
print(ele)
Beautifulsoup4 只获取所有的超链接
# 设定网址
url = "https://www.xyblog.cc/"
# 获取网页html
r = req.get(url)
# 导入 html 进入 beautifulsoup4
soup = BeautifulSoup(r.text, features="html.parser")
# 只获取所有的超链接
for ele in soup.find_all("a"):
print(ele)
Beautifulsoup4 使用 id 获取元素
# 设定网址
url = "https://www.xyblog.cc/"
# 获取网页html
r = req.get(url)
# 导入 html 进入 beautifulsoup4
soup = BeautifulSoup(r.text, features="html.parser")
# 使用 id 获取元素
for ele in soup.select("#main_header"):
print(ele)
Beautifulsoup4 使用 class 获取元素
# 设定网址
url = "https://www.xyblog.cc/"
# 获取网页html
r = req.get(url)
# 导入 html 进入 beautifulsoup4
soup = BeautifulSoup(r.text, features="html.parser")
# 使用 class 获取元素
for ele in soup.select(".mt-2"):
print(ele)