在Python中读取网页标题,你可以使用`requests`和`BeautifulSoup`库。以下是使用这些库获取网页标题的步骤:
1. 安装`requests`和`BeautifulSoup`库(如果尚未安装):
pip install requests beautifulsoup4
2. 使用以下代码读取网页标题:
import requestsfrom bs4 import BeautifulSoup指定要读取的网页URLurl = 'https://www.example.com'发送HTTP GET请求response = requests.get(url)使用BeautifulSoup解析HTML内容soup = BeautifulSoup(response.content, 'html.parser')提取网页标题title = soup.title.string打印网页标题print(title)
这段代码会发送一个GET请求到指定的URL,然后使用BeautifulSoup解析返回的HTML内容,并提取`

