如何删除<;表>;从这个案例中使用python构造?
如何使用python从HTML中删除“表” 我遇到过这样的情况:如何删除<;表>;从这个案例中使用python构造?,python,python-2.7,beautifulsoup,Python,Python 2.7,Beautifulsoup,如何使用python从HTML中删除“表” 我遇到过这样的情况: paragraph = ''' <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem molestiae consequuntur officiis corporis sint.<br /><br /> <table> <tr> <td> text title </
paragraph = '''
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem molestiae consequuntur officiis corporis sint.<br /><br />
<table>
<tr>
<td>
text title
</td>
<td>
text title 2
</td>
</tr>
</table>
<p> lorem ipsum</p>
'''
段落=“”
Lorem ipsum dolor sit amet,奉献精英。圣卢西亚大学医学院
文本标题
正文标题2
同侧眼睑
'''
如何使用python删除具有上述表结构的内容?
我希望产生的结果如下:
paragraph = '''
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem molestiae consequuntur officiis corporis sint.<br /><br />
<p> lorem ipsum</p>
'''
段落=“”
Lorem ipsum dolor sit amet,奉献精英。圣卢西亚大学医学院
同侧眼睑
'''
您可以使用
[16]中的:来自bs4导入BeautifulSoup
在[17]中:汤=美丽的汤(“Lorem ipsum door sit amet,concetetur adipising elit.Quidem molestiae consequentius officius corporis sint.
....:
....:
....:
..:文本标题或一些
....:
....:
....:
(“lorem ipsum”)
在[18]中:z=soup.table.extract()
在[19]中:汤
出[19]:
Lorem ipsum dolor sit amet,奉献精英。圣卢西亚大学医学院
同侧眼睑
您可以使用
[16]中的:来自bs4导入BeautifulSoup
在[17]中:汤=美丽的汤(“Lorem ipsum door sit amet,concetetur adipising elit.Quidem molestiae consequentius officius corporis sint.
....:
....:
....:
..:文本标题或一些
....:
....:
....:
(“lorem ipsum”)
在[18]中:z=soup.table.extract()
在[19]中:汤
出[19]:
Lorem ipsum dolor sit amet,奉献精英。圣卢西亚大学医学院
同侧眼睑
您也可以尝试这种基本的字符串格式
paragraph = paragraph[:paragraph.find('<table>')] + # Find the starting letter of '<table>'
paragraph[paragraph.find('</table>')+ # Find the starting letter of </table>
(len('<\table>')+1):] # Add 1 because length starts from zero
print(paragraph)
段落=段落[:段落.查找(“”)]+#查找“”的起始字母
段落[段落.查找('')+#查找
(len(“”)+1):]#添加1,因为长度从零开始
打印(段落)
即使这项工作用于基本文本提取您也可以尝试这种基本的字符串格式
paragraph = paragraph[:paragraph.find('<table>')] + # Find the starting letter of '<table>'
paragraph[paragraph.find('</table>')+ # Find the starting letter of </table>
(len('<\table>')+1):] # Add 1 because length starts from zero
print(paragraph)
段落=段落[:段落.查找(“”)]+#查找“”的起始字母
段落[段落.查找('')+#查找
(len(“”)+1):]#添加1,因为长度从零开始
打印(段落)
即使是这项用于基本文本提取的工作使用正则表达式也很复杂,这是我建议的一种愚蠢的方法:
def remove_table(s):
left_index = s.find('<table>')
if -1 == left_index:
return s
right_index = s.find('</table>', left_index)
return s[:left_index] + remove_table(s[right_index + 8:])
def删除表格:
左索引=s.find(“”)
如果-1==左索引:
返回s
右索引=s.find(“”,左索引)
返回s[:左索引]+删除表(s[右索引+8:])
结果中可能有一些空行。使用regex很复杂,我建议这样做很愚蠢:
def remove_table(s):
left_index = s.find('<table>')
if -1 == left_index:
return s
right_index = s.find('</table>', left_index)
return s[:left_index] + remove_table(s[right_index + 8:])
def删除表格:
左索引=s.find(“”)
如果-1==左索引:
返回s
右索引=s.find(“”,左索引)
返回s[:左索引]+删除表(s[右索引+8:])
结果中可能有一些空行。你能发布你尝试过的代码吗?它是否总是以开头和结尾,你能使用regexCould你发布你尝试过的代码吗?它是否总是以开头和结尾,你能使用regexthanks作为你的答案,但我正在寻找一个不使用模块的解决方案。也许可以使用正则表达式或其他方法。但你的答案将是我的选择能力。@AriPrihantoro你不想使用
regex
为什么?因为这将使我寻找另一种方法,而不是将模块用作BeautifulSoup或其他方法。我更喜欢使用regex之类的方法。也许你误解了我的观点。谢谢你的回答,但我正在寻找一个不使用模块的解决方案。也许可以使用正则表达式或其他方法。但你的答案将是我的选择能力。@AriPrihantoro你不想使用regex
为什么?因为这将使我寻找另一种方法,而不是将模块用作BeautifulSoup或其他方法。我更喜欢使用regex之类的方法。也许你误解了我的观点。是的,我会这样问。你会有很多s吗?不是他的表的数量,如果我问,如果表的列数超过1。是的,我会这样问。你会有很多s吗?不是他的表的数量,如果我问,如果表的列数超过1。谢谢你的回答,我尝试你的源代码是工作。你的答案符合我的情况。谢谢你的回答,我尝试你的源代码正在工作。你的回答符合我的情况。