python获取http网页标题

 

def get_title(url):
    s = requests.session()
    response = request.urlopen(url)
    html = response.read()
    charset = chardet.detect(html)['encoding']  # 对该html进行编码的获取
    result = s.get(url)
    if (charset == "GB2312" or charset is None):
        result.encoding = 'gbk'
    else:
        result.encoding = 'utf-8'
    content = result.text
    title = re.findall('(.*)', content)[0]
    return title

 

你可能感兴趣的:(python)