1

我尝试用 html 表单解析一些网站。如果只有一个打开和一个关闭窗体-Tag,则没有问题。我首先通过解析http://www.w3schools.com/html/html_forms.asp意识到了这个问题

如果有 2 个或更多表单标签,我会出现奇怪的行为,所有关闭的表单标签都将移动到文档的末尾。有人有同样的问题吗?

这是一个基本的示例网页:

<!DOCTYPE html>
<html lang="en-US">
<head>
<title>HTML Forms and Input</title>
</head>
<body>
<p>stuff and so on</p>
<form>
First name: <input type="text" name="firstname" size="20"><br>
Last name: <input type="text" name="lastname" size="20">
</form>
<p>some text</p>
<form>
First name: <input type="text" name="firstname" size="20"><br>
Last name: <input type="text" name="lastname" size="20">
</form>
</body>
</html>

这是代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import urllib2
from bs4 import BeautifulSoup
lSoup = BeautifulSoup(open("forms2.html"))
print lSoup

这就是我得到的:

<!DOCTYPE html>
<html lang="en-US"><head>
<title>HTML Forms and Input</title>
</head>
<body>
<p>stuff and so on</p>
<form>
First name: <input name="firstname" size="20" type="text"/><br/>
Last name: <input name="lastname" size="20" type="text"/>
<p>some text</p>
<form>
First name: <input name="firstname" size="20" type="text"/><br/>
Last name: <input name="lastname" size="20" type="text"/>
</form></form></body></html>

有任何想法吗?

感谢帮助!

4

0 回答 0