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

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

    Here's a simplified version of @Vinko Vrsalovic's answer:

    import urllib2
    from BeautifulSoup import BeautifulSoup
    
    soup = BeautifulSoup(urllib2.urlopen("https://www.google.com"))
    print soup.title.string
    

    NOTE:

    • soup.title finds the first title element anywhere in the html document

    • title.string assumes it has only one child node, and that child node is a string

    For beautifulsoup 4.x, use different import:

    from bs4 import BeautifulSoup
    

提交回复
热议问题