Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从html文件中提取文本会导致属性错误_Python_Html_Beautifulsoup - Fatal编程技术网

Python 从html文件中提取文本会导致属性错误

Python 从html文件中提取文本会导致属性错误,python,html,beautifulsoup,Python,Html,Beautifulsoup,我正试图从我的jupyter笔记本上的htm文件中提取文本。我首先使用以下方法读取文件: 打开('Materials.htm')作为文件b的: file3=b.readlines() file3=''.join(file3) 然后,我解析文件并使用get_text() 此代码会打印列表,但也会给出 AttributeError:“非类型”对象没有属性“获取文本” 我想把它放在一个for循环中读取不同的文件,但由于错误,它不起作用。有人知道我做错了什么吗?比你强 您应该按原样传递file对象,以美

我正试图从我的jupyter笔记本上的htm文件中提取文本。我首先使用以下方法读取文件: 打开('Materials.htm')作为文件b的
:
file3=b.readlines()
file3=''.join(file3)

然后,我解析文件并使用get_text()

此代码会打印列表,但也会给出

AttributeError:“非类型”对象没有属性“获取文本”


我想把它放在一个for循环中读取不同的文件,但由于错误,它不起作用。有人知道我做错了什么吗?比你强

您应该按原样传递file对象,以美化组并将其解析为HTML

with open('Materials.htm','r') as f:
    Stock_page = BeautifulSoup(f, "html.parser")

您应该按原样传递file对象以美化组并将其解析为HTML

with open('Materials.htm','r') as f:
    Stock_page = BeautifulSoup(f, "html.parser")
我正在执行“”join(),因为与open一起使用会给我一个字符串列表,我无法将这些字符串提供给beautifulsoup。我试着用“html.parser”来代替它,它成功了。谢谢你的帮助!我正在执行“”join(),因为与open一起使用会给我一个字符串列表,我无法将这些字符串提供给beautifulsoup。我试着用“html.parser”来代替它,它成功了。谢谢你的帮助!