如何使用Python中的Beautiful Soup和遍历从网页中获取标题

2024-05-29 04:31:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中使用bs4来解析web页面并获取信息。我很难仅仅抓住标题。另一个我很纠结的部分是跟踪链接,这应该递归地完成还是通过循环来完成

def getTitle(link):
    resp = urllib.request.urlopen(link)
    soup = BeautifulSoup(resp, 'html.parser')
    print(soup.find("<title>"))

Tags: web标题链接requestdefhtmllink页面
1条回答
网友
1楼 · 发布于 2024-05-29 04:31:06
from bs4 import BeautifulSoup    
import urllib    

def getTitle(link):
    resp = urllib.request.urlopen(link)
    soup = BeautifulSoup(resp, 'html.parser')
    return soup.title.text

print(getTitle('http://www.bbc.co.uk/news'))

其中显示:

Home - BBC News

相关问题 更多 >

    热门问题