如何使用python和beautifulsoup解析脚本标记

如何使用python和beautifulsoup解析脚本标记,python,beautifulsoup,Python,Beautifulsoup,我试图提取文档中的框架标记的属性。在页面上编写函数,如下所示: <script language="javascript"> . . . document.write('<frame name="nav" src="/nav/index_nav.html" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" border = "no" noresize>'); if (anchor != "")

我试图提取
文档中的框架标记的属性。在页面上编写
函数,如下所示:

<script language="javascript">
.
.
.
document.write('<frame name="nav" src="/nav/index_nav.html" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" border = "no" noresize>');
 if (anchor != "") {
  document.write('<frame name="body" src="http://content.members.fidelity.com/mfl/summary/0,,' + cusip + ',00.html?' + anchor + '" marginwidth="0" marginheight="0" scrolling="auto" frameborder="0" noresize>');
 } else {
  document.write('<frame name="body" src="http://content.members.fidelity.com/mfl/summary/0,,' + cusip + ',00.html" marginwidth="0" marginheight="0" scrolling="auto" frameborder="0" noresize>');
 }
 document.write('</frameset>');


// end hiding -->
</script>

.
.
.
文件。写(“”);
如果(锚定!=“”){
文件。写(“”);
}否则{
文件。写(“”);
}
文件。写(“”);
//结束隐藏-->
findAll('frame')
方法没有帮助。有没有办法读取框架标签的内容

我使用的是python 2.5和BeautifulSoup 3.0.8

我也愿意将Python3.1与Beautifulsoup3.1结合使用 只要我能得到结果


谢谢

单靠BeautifulSoup是做不到的。BeautifulSoup解析HTML就像它到达浏览器一样(在任何重写或DOM操作之前),它不解析(更不用说执行)Javascript


在这种特殊情况下,您可能需要使用一个简单的正则表达式。

Pyparsing可能会帮助您将JS和HTML结合起来。此解析器查找
文档。编写包含带引号的字符串或多个带引号的字符串和标识符的字符串表达式的
语句,对字符串表达式进行准求值,对其进行分析以查找嵌入的
标记,并将帧属性作为PyParseResults对象返回,这使您能够访问命名属性,就像它们是对象属性或dict键一样(您的首选项)

jssrc=”“”
.
.
.
文件。写(“”);
如果(锚定!=“”)
{document.write(“”);}
其他的
{document.write(“”);}
文件。写(“”);
//结束隐藏-->
"""
从pyparsing导入*
#定义一些基本标点符号和带引号的字符串
LPAR,RPAR,PLUS=map(抑制“()+”)
qs=QuotedString(“”)
#使用pyparsing helper定义用于打开的表达式
#标记,其中还包括对属性的支持
frameTag=makeHTMLTags(“帧”)[0]
#我们的一些document.write语句不包含sting文字,
#但是字符串和变量加在一起的表达式;定义
#一个标识符表达式,并添加一个转换
#将变量名称转换为可能的值
ident=Word(alphas).setParseAction(lambda-toks:evalvars[toks[0]])
evalvars={'cusip':'cusip','anchor':'anchor}
#现在将字符串表达式本身定义为带引号的字符串,
#(可选)后跟添加的标识符和带引号的字符串
#一起;标识符将被转换为其定义的值
#当它们被解析时;stringExpr上的第一个解析操作连接
#所有代币;然后第二个解析操作实际解析
#字符串的主体作为标记,并返回解析结果
#标签及其属性;如果解析失败(即
#字符串包含非标记的内容),第二个
#解析操作将引发异常,这将导致stringExpr
#表达失败
stringExpr=qs+ZeroOrMore(加+(标识| qs))
stringExpr.setParseAction(lambda-toks:''.join(toks))
stringExpr.addParseAction(lambda toks:
frameTag.parseString(toks[0],parseAll=True))
#最后,定义整个document.write(…)表达式
docWrite=“document.write”+LPAR+stringExpr+RPAR
#扫描源文件,查找document.write命令,其中包含
#使用扫描字符串的标签;打印原始源片段,
#然后访问从标记中提取的一些属性
#在带引号的字符串中,使用对象属性表示法或
#dict索引表示法
对于docWrite.scanString(jssrc)中的dw、locstart和locend:
打印jssrc[locstart:locend]
打印dw.name
打印dw[“src”]
打印
印刷品:

document.write('<frame name="nav" src="/nav/index_nav.html" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" border = "no" noresize>')
nav
/nav/index_nav.html

document.write('<frame name="body" src="http://content.members.fidelity.com/mfl/summary/0,,' + cusip + ',00.html?' + anchor + '" marginwidth="0" marginheight="0" scrolling="auto" frameborder="0" noresize>')
body
http://content.members.fidelity.com/mfl/summary/0,,CUSIP,00.html?ANCHOR

document.write('<frame name="body" src="http://content.members.fidelity.com/mfl/summary/0,,' + cusip + ',00.html" marginwidth="0" marginheight="0" scrolling="auto" frameborder="0" noresize>')
body
http://content.members.fidelity.com/mfl/summary/0,,CUSIP,00.html
document.write(“”)
导航
/nav/index_nav.html
文件。写入(“”)
身体
http://content.members.fidelity.com/mfl/summary/0,CUSIP,00.html?锚定
文件。写入(“”)
身体
http://content.members.fidelity.com/mfl/summary/0,CUSIP,00.html
document.write('<frame name="nav" src="/nav/index_nav.html" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" border = "no" noresize>')
nav
/nav/index_nav.html

document.write('<frame name="body" src="http://content.members.fidelity.com/mfl/summary/0,,' + cusip + ',00.html?' + anchor + '" marginwidth="0" marginheight="0" scrolling="auto" frameborder="0" noresize>')
body
http://content.members.fidelity.com/mfl/summary/0,,CUSIP,00.html?ANCHOR

document.write('<frame name="body" src="http://content.members.fidelity.com/mfl/summary/0,,' + cusip + ',00.html" marginwidth="0" marginheight="0" scrolling="auto" frameborder="0" noresize>')
body
http://content.members.fidelity.com/mfl/summary/0,,CUSIP,00.html