Python I';I’’我试图用BeautifulSoup获取这个网站的标题,但我一直遇到一个类型错误,我该如何修复它?

Python I';I’’我试图用BeautifulSoup获取这个网站的标题,但我一直遇到一个类型错误,我该如何修复它?,python,beautifulsoup,Python,Beautifulsoup,我在课堂上也写过类似的代码,不过我想自己制作一个网站,以获得网站的名称和NBA日报的头号得分手。但是我遇到的第一个stwp是获取页面标题。您可以使用下面的代码获取页面标题 import requests import bs4 as BeautifulSoup page = requests.get('https://www.basketball-reference.com/friv/dailyleaders.fcgi') soup = BeautifulSoup( page.content,

我在课堂上也写过类似的代码,不过我想自己制作一个网站,以获得网站的名称和NBA日报的头号得分手。但是我遇到的第一个stwp是获取页面标题。

您可以使用下面的代码获取页面标题

import requests
import bs4 as BeautifulSoup

page = requests.get('https://www.basketball-reference.com/friv/dailyleaders.fcgi')
soup = BeautifulSoup( page.content, 'html-parser')

print (soup.title)

除了bs4之外,还可以使用lxml的tree.xpath来获取标题。希望这有帮助。谢谢

请检查答案,如果有帮助,请正确勾选,因为它将作为其他人的参考,谢谢
import requests
from bs4 import BeautifulSoup

url_req = requests.get('https://www.basketball-reference.com/friv/dailyleaders.fcgi')
soup = BeautifulSoup(url_req.content, 'lxml')
print(soup.select_one('title').text)