python - 如何使用 Python 检索网页的页面标题?

标签 python html

如何使用 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/

相关文章:

Python Opencv SolvePnP 产生错误的翻译向量

python - 在多个参数上使用 scipy minimize 时将常量参数传递给函数

javascript - 我想创建一个函数来获取任何输入传递 Id 的值

jquery - jQuery 中的调整按钮大小?

python - 使用selenium循环链接并获取页面源

python - django 中两个查询集的交集

Python Flask API - 发送和接收字节数组和元数据

html - 拉伸(stretch) div 高度到下一个 div 的开始

javascript - 从外部js文件获取html文件的元素

html - 在没有 javascript 的情况下显示隐藏的图像