-
-
Save tsoporan/839901 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2,re | |
def url_title(url,**kwargs): | |
request = urllib2.Request(url) | |
try: | |
data = urllib2.urlopen(request) | |
except urllib2.HTTPError: | |
data = None | |
except urllib2.URLError: | |
data = None | |
except httplib.BadStatusLine: | |
data = None | |
except httplib.InvalidURL: | |
data = None | |
if data: | |
for line in data: | |
if '<title>' in line: | |
headers = data.info() | |
raw_encoding = headers['content-type'].split('charset=')[-1] | |
if 'text/html' in raw_encoding: | |
encoding = 'utf8' | |
else: | |
encoding = raw_encoding | |
title_search = re.search('(?<=<title>).*(?=<\/title>)',line) | |
if title_search: | |
try: | |
if encoding: | |
title = unicode(title_search.group(0),encoding) | |
else: | |
title = title_search.group(0) | |
except Exception, e: | |
print(e) | |
title = None | |
return "%s | %s" % (encoding,title) | |
sample_urls = [ | |
'http://news.cnet.com/8301-31021_3-20035058-260.html?part=rss&subj=news&tag=2547-1_3-0-20', # ISO-8859-1 | |
'http://www.androidphonethemes.com/nec-medias-n-04c-is-7-7mm-think-%e2%80%93-runs-android-2-2/', # UTF-8 | |
'http://www.voiceblog.jp/semaasa/', # EUC-JP | |
'http://taisyo.seesaa.net/article/187352622.html', # Shift_JIS | |
'http://www.tuaw.com/2011/02/22/element-vapor-pro-iphone-4-case-exclusive-first-look-and-giveaw/', # text/html (does not contain unicode chars) | |
'http://www.atpress.ne.jp/view/19160', # text/html (contains unicode) | |
'http://we-newsing.com/archives/128', # text/html (contains unicode) | |
'http://live.nicovideo.jp/watch/lv41334754', # text/html (contains unicode) | |
'http://www.superdownloads.com.br/materias/acionistas-da-apple-querem-substituto-steve-jobs.html', # utf8 (claims utf8 but it LIES) | |
] | |
for url in sample_urls: | |
print url_title(url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment