import requests
from bs4 import BeautifulSoup
# 发送HTTP请求获取网页内容
url = "https://example.com/"
response = requests.get(url, verify=False) # 避免SSLError
html_content = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, "html.parser")
# 找到特定的元素或数据
title = soup.title.text
links = soup.find_all("a")
# 打印结果
print("网页标题:", title)
print("所有链接:")
for link in links:
print(link.get("href"))
在这个示例中,我们首先使用requests库发送HTTP请求来获取网页的内容。然后,我们使用BeautifulSoup库将网页内容解析为一个可操作的对象。接下来,我们可以使用BeautifulSoup提供的方法和函数来查找特定的元素或数据。在这个示例中,我们找到了网页的标题和所有的链接,并将它们打印出来。