Python 获取 <title> [英] Python fetching <title>
本文介绍了Python 获取 <title>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取我使用 urllib2 打开的网页的标题.什么是最好的方法来做到这一点,解析 html 并找到我需要的东西(现在只有 -tag,但将来可能需要更多).
I want to fetch the title of a webpage which I open using urllib2. What is the best way to do this, to parse the html and find what I need (for now only the -tag but might need more in the future).
是否有一个很好的解析库用于这个目的?
Is there a good parsing lib for this purpose?
推荐答案
是的,我会推荐 BeautifulSoup一个>
如果您要获得标题,那就是:
If you're getting the title it's simply:
soup = BeautifulSoup(html)
myTitle = soup.html.head.title
或
myTitle = soup('title')
取自文档
它非常健壮,无论多么混乱都会解析 html.
It's very robust and will parse the html no matter how messy it is.
这篇关于Python 获取 <title>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文