Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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/7/elixir/2.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-beautifulsoup-如何处理缺少的结束标记_Python_Beautifulsoup_Html Table - Fatal编程技术网

Python-beautifulsoup-如何处理缺少的结束标记

Python-beautifulsoup-如何处理缺少的结束标记,python,beautifulsoup,html-table,Python,Beautifulsoup,Html Table,我想使用beautifulsoup从html代码中删除该表。下面显示了html的一个片段。使用table.findAll('tr')时,我得到的是整个表,而不仅仅是行。(可能是因为html代码中缺少结束标记?) 正如他们的html5lib中所述,它像web浏览器一样解析文档(在本例中类似于lxml)。它将尝试在需要时通过添加/关闭标记来修复您的文档树 在您的示例中,我使用lxml作为解析器,它给出了以下结果: soup = BeautifulSoup(data, "lxml") table =

我想使用beautifulsoup从html代码中删除该表。下面显示了html的一个片段。使用
table.findAll('tr')
时,我得到的是整个表,而不仅仅是行。(可能是因为html代码中缺少结束标记?)

正如他们的
html5lib
中所述,它像web浏览器一样解析文档(在本例中类似于
lxml
)。它将尝试在需要时通过添加/关闭标记来修复您的文档树

在您的示例中,我使用lxml作为解析器,它给出了以下结果:

soup = BeautifulSoup(data, "lxml")
table = soup.findAll("table")[0]
rows = table.find_all('tr')
for tr in rows:
    print(tr.get_text(strip=True))
请注意,
lxml
添加了html和body标记,因为它们在源代码中不存在(它将尝试创建一个格式良好的文档,如前所述)

     soup = BeautifulSoup(data, "html.parser")
     table = soup.findAll("table")[0]
     rows = table.find_all('tr')
     for tr in rows:
         print(tr.text)
soup = BeautifulSoup(data, "lxml")
table = soup.findAll("table")[0]
rows = table.find_all('tr')
for tr in rows:
    print(tr.get_text(strip=True))