How can I retrieve the page title of a webpage using Python?

后端 未结 11 1765
南笙
南笙 2020-12-07 08:55

How can I retrieve the page title of a webpage (title html tag) using Python?

11条回答
  •  一向
    一向 (楼主)
    2020-12-07 09:39

    I'll always use lxml for such tasks. You could use beautifulsoup as well.

    import lxml.html
    t = lxml.html.parse(url)
    print t.find(".//title").text
    

    EDIT based on comment:

    from urllib2 import urlopen
    from lxml.html import parse
    
    url = "https://www.google.com"
    page = urlopen(url)
    p = parse(page)
    print p.find(".//title").text
    

提交回复
热议问题