2009-12-10 8 views
1

나는 다음과 같이 페이지에 document.write 기능 내부 프레임 태그의 속성을 추출하려고 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 != "") { 
    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') 방법은 도움이되지 않았다. 프레임 태그의 내용을 읽을 수있는 방법이 있습니까?

저는 파이썬 2.5와 BeautifulSoup 3.0.8을 사용하고 있습니다.

나는 결과를 얻을 수있는 한 BeautifulSoup 3.1 과 함께 python 3.1을 사용하고 있습니다.

감사

답변

2

는 당신은 혼자가 BeautifulSoup로 함께 할 수 없습니다. BeautifulSoup은 HTML을 파싱하여 (재 작성 또는 DOM 조작 전에) 브라우저에 도착하고, Javascript를 파싱하지는 않습니다.

이 특별한 경우에는 간단한 정규식을 사용하는 것이 좋습니다.

+0

확인 감사 – qaAutomation

1

파이핑은 JS와 HTML의 혼합을 연결하는 데 도움이 될 수 있습니다. 이 구문 분석기는 인용 문자열 또는 인용 된 여러 문자열과 식별자의 문자열 표현을 포함하는 document.write 문을 찾고 문자열 표현을 준 평가하고 포함 된 <frame> 태그에 대해 파싱 한 다음 프레임 속성을 psearsing ParseResults 객체로 반환합니다. 객체 속성 또는 dict 키 (사용자 환경 설정) 인 것처럼 명명 된 속성에 액세스 할 수 있습니다.

jssrc = """ 
<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>""" 

from pyparsing import * 

# define some basic punctuation, and quoted string 
LPAR,RPAR,PLUS = map(Suppress,"()+") 
qs = QuotedString("'") 

# use pyparsing helper to define an expression for opening <frame> 
# tags, which includes support for attributes also 
frameTag = makeHTMLTags("frame")[0] 

# some of our document.write statements contain not a sting literal, 
# but an expression of strings and vars added together; define 
# an identifier expression, and add a parse action that converts 
# a var name to a likely value 
ident = Word(alphas).setParseAction(lambda toks: evalvars[toks[0]]) 
evalvars = { 'cusip' : "CUSIP", 'anchor' : "ANCHOR" } 

# now define the string expression itself, as a quoted string, 
# optionally followed by identifiers and quoted strings added 
# together; identifiers will get translated to their defined values 
# as they are parsed; the first parse action on stringExpr concatenates 
# all the tokens; then the second parse action actually parses the 
# body of the string as a <frame> tag and returns the results of parsing 
# the tag and its attributes; if the parse fails (that is, if the 
# string contains something that is not a <frame> tag), the second 
# parse action will throw an exception, which will cause the stringExpr 
# expression to fail 
stringExpr = qs + ZeroOrMore(PLUS + (ident | qs)) 
stringExpr.setParseAction(lambda toks : ''.join(toks)) 
stringExpr.addParseAction(lambda toks: 
    frameTag.parseString(toks[0],parseAll=True)) 

# finally, define the overall document.write(...) expression 
docWrite = "document.write" + LPAR + stringExpr + RPAR 

# scan through the source looking for document.write commands containing 
# <frame> tags using scanString; print the original source fragment, 
# then access some of the attributes extracted from the <frame> tag 
# in the quoted string, using either object-attribute notation or 
# dict index notation 
for dw,locstart,locend in docWrite.scanString(jssrc): 
    print jssrc[locstart:locend] 
    print dw.name 
    print dw["src"] 
    print 

인쇄 : 나는 것을 시도 할 것이다

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 
관련 문제