<input type=submit value=百度一下 id=sb>
很常见的问题,标准的HTML应该是:<input type="submit" value="百度一下" id="sb">
,不知道是百度出于压缩考虑,还是它的技术人员偷懒
应该用 tidy 之类的工具修补下,然后再用 BeautifulSoup 处理
不过正如之前我那条不被人关注的帖子中所提到的,原有多个对 tidy 的 python 封装都不好用
张教主推荐了个自己封装的 tidy ,可以试下
刚刚在转向SGMLlib~
thx,明白了
这种不标准的网页比例太高的话,beautifulSoup的实用性就降低了
刚刚在转向SGMLlib~
长时间使用会导致进程crash,没有找到原因。
不过可以干脆用原始的版本,搞一个进程外调用吧
wget http://nchc.dl.sourceforge.net/sourceforge/tidy/tidy4aug00.tgz
然后安装,然后
from __future__ import with_statement
import subprocess
import os
def tidy(html):
with os.tmpfile() as temp:
with open(os.devnull,"w" ) as null:
print >>temp,html
temp.seek(0)
html=subprocess.Popen(
["tidy", "-utf8","-asxhtml"],
stdin=temp,
stderr=null,
stdout=subprocess.PIPE
).communicate()[0]
begin="<body>"
return html[html.find(begin)+len(begin):html.rfind("</body>")].strip()
tidy("<div>x<a>a")