python + urllib + BeautifulSoup 获取百度首页标题
1、urllib库是python自带的库,不需要安装。
2、BeautifulSoup是第三方库,安装方法: pip install bs4
# coding = utf-8
from urllib.request import urlopen
from bs4 import BeautifulSoup
# 请求获取html
html = urlopen('http://www.baidu.com/')
# 用BeautifulSoup解析html
res = BeautifulSoup(html.read(), 'html.parser')
# 获取标题
title = res.head.title.text
# 打印标题
print(title)