首页 > 解决方案 > 当我尝试使用漂亮的汤打印标签时显示 IdentationError

问题描述

from bs4 import BeautifulSoup

with open('Home.html','r')as html_file:
    content=html_file.read()
    print(content)

    soup = BeautifulSoup(content,'lxml')
   tags = soup.find('h5') #error showing here
   print(tags)#and here

谁能告诉我为什么从这里显示识别错误。请告诉我我必须安装哪个软件包或运行这个软件包我必须做什么样的事情。

标签: pythonweb-scraping

解决方案


您不小心将 with 语句的前 4 行缩进了 1 个空格。
这是一个修复:

from bs4 import BeautifulSoup

with open('Home.html','r')as html_file:
   content=html_file.read()
   print(content)

   soup = BeautifulSoup(content,'lxml')
   tags = soup.find('h5') 
   print(tags)

推荐阅读