How to get page title in requests

后端 未结 5 1594
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 22:53

What would be the simplest way to get the title of a page in Requests?

r = requests.get(\'http://www.imdb.com/title/tt0108778/\')
# ? r.title
Friends (TV Ser         


        
5条回答
  •  有刺的猬
    2020-12-28 23:10

    No need to import other libraries. Request has this functionality in-built.

    >>> hearders = {'headers':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0'}
    >>> n = requests.get('http://www.imdb.com/title/tt0108778/', headers=hearders)
    >>> al = n.text
    >>> al[al.find('') + 7 : al.find('')]
    u'Friends (TV Series 1994\u20132004) - IMDb'
    

    Update after ZN13's comment

    >>> import re
    >>> import requests
    >>> n = requests.get('https://www.libsdl.org/release/SDL-1.2.15/docs/html/guideinputkeyboard.html')
    >>> al = n.text
    >>> d = re.search('<\W*title\W*(*)>> d.group(1)
    u'Handling the Keyboard'
    

    This will work for all cases whether extra non alphabetical characters are present with title tag or not.

提交回复
热议问题