用python获取url标题

时间:2012-12-27 13:11:28

标签: python url title

我想问有没有办法用python获取url标题?如果是,简单的代码或任何想法如何做到这一点。谢谢!

1 个答案:

答案 0 :(得分:2)

使用BeautifulSoup和urllib2.urlopen你可以尝试:

import urllib2
from BeautifulSoup import BeautifulSoup

#fetch html
source = urllib2.urlopen(url)

#parse with BeautifulSoup
BS = BeautifulSoup(source)

#search for title element, print element.text
print BS.find('title').text