如何使用 Python 检索网页的页面标题(title html 标签)?
最佳答案
这里是 @Vinko Vrsalovic's answer 的简化版本:
import urllib2
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen("https://www.google.com"))
print soup.title.string
注意:
soup.title 在 html 文档中找到第一个 title 元素 anywhere
title.string 假设它只有 一个 子节点,并且该子节点是一个 string
对于 beautifulsoup 4.x ,使用不同的导入:
from bs4 import BeautifulSoup
关于python - 如何使用 Python 检索网页的页面标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51233/