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

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

    This is probably overkill for such a simple task, but if you plan to do more than that, then it's saner to start from these tools (mechanize, BeautifulSoup) because they are much easier to use than the alternatives (urllib to get content and regexen or some other parser to parse html)

    Links: BeautifulSoup mechanize

    #!/usr/bin/env python
    #coding:utf-8
    
    from BeautifulSoup import BeautifulSoup
    from mechanize import Browser
    
    #This retrieves the webpage content
    br = Browser()
    res = br.open("https://www.google.com/")
    data = res.get_data() 
    
    #This parses the content
    soup = BeautifulSoup(data)
    title = soup.find('title')
    
    #This outputs the content :)
    print title.renderContents()
    

提交回复
热议问题