Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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 如何提取<;车身>;标签?_Python - Fatal编程技术网

Python 如何提取<;车身>;标签?

Python 如何提取<;车身>;标签?,python,Python,在不依赖Lxml或BeautifulSoup的情况下,从HTML页面中提取body标记内容的好方法是什么 我正在为Django编写一个附加程序包,对于这样一个小任务,我不想在我的附加程序中添加另一个依赖项。使用我提到的其中一个库非常容易,但除了这个和regexs之外,我想不出其他方法。这是一个非常粗糙的方法,我确信它是完全脆弱的(不考虑出现在实际的标记中的,等等),但是如果你绝对不能使用上述库,也许是这样的 In [7]: s = '<html><head>More st

在不依赖Lxml或BeautifulSoup的情况下,从HTML页面中提取
body
标记内容的好方法是什么


我正在为Django编写一个附加程序包,对于这样一个小任务,我不想在我的附加程序中添加另一个依赖项。使用我提到的其中一个库非常容易,但除了这个和regexs之外,我想不出其他方法。

这是一个非常粗糙的方法,我确信它是完全脆弱的(不考虑出现在实际的
标记中的
,等等),但是如果你绝对不能使用上述库,也许是这样的

In [7]: s = '<html><head>More stuff</head><body>Text inside of the body</body>Random text</html>'

In [8]: s.split('<body>')[1].split('</body>')[0]
Out[8]: 'Text inside of the body'
[7]中的
s='正文中的更多填充文本随机文本'
在[8]中:s.split(“”)[1]。split(“”)[0]
Out[8]:“正文内的文本”
如果实际身体中的标签是一个问题,那么这个讨厌的东西似乎起作用了:

In [1]: s = '<html><head>More stuff</head><body>Text inside of the body<body>more sample text</body>and then more text and then another<body> and then another </body> and then end</body>Random text</html>'

In [2]: '</body>'.join('<body>'.join(s.split('<body>')[1:]).split('</body>')[:-1])
Out[2]: 'Text inside of the body<body>more sample text</body>and then more text and then another<body> and then another </body> and then end'
[1]中的
s]:s='正文中的更多填充文本更多示例文本,然后是更多文本,然后是另一个,然后是另一个,然后是endRandom文本'
在[2]:“”.join(“”.join(s.split(“”)[1:]).split(“”)[:-1])
Out[2]:“正文中的文本更多示例文本,然后更多文本,然后是另一个,然后是另一个,然后是结束”

Python附带了一个.Huh。我不知道我怎么会错过那个。它看起来比使用字符串函数更好。@Katrielex,您愿意提供一个示例,说明如何使用
HTMLParser
模块实现这一点吗?我试过了文档,但是完全迷路了。我会试着给你写一个——但老实说,我会用BeautifulSoup。它可以在封面下使用
HTMLParser
(所以你不需要lxml),而且它对你在“野外”看到的HTML类型更有弹性。真的有可能有多个body标签吗?@MarkRansom我想我在想,如果它包含在代码片段或类似的东西中(我想这些会被转义)@Mark:解析HTML的部分问题在于世界上有多少格式不正确的HTML。@Katrielex:非常好。在这种情况下,您也应该为缺少的
或标记中的空格做好准备,或者…@MarkRansom是的-我肯定默认使用除此之外的任何东西:)