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

后端 未结 11 1787
南笙
南笙 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:40

    Using lxml...

    Getting it from page meta tagged according to the Facebook opengraph protocol:

    import lxml.html.parse
    html_doc = lxml.html.parse(some_url)
    
    t = html_doc.xpath('//meta[@property="og:title"]/@content')[0]
    

    or using .xpath with lxml:

    t = html_doc.xpath(".//title")[0].text
    

提交回复
热议问题