Python:如何提取嵌入在html文件中的xml?
我有一个嵌入xml的html文件,源代码粘贴在pastbin中:Python:如何提取嵌入在html文件中的xml?,python,xml,screen-scraping,Python,Xml,Screen Scraping,我有一个嵌入xml的html文件,源代码粘贴在pastbin中: <html> <head> <title> test֤</title> </head> <body> <form name="acsForm" action="" method="post" > <textarea rows=10 cols=80 name="xmlText"><?xml v
<html>
<head>
<title> test֤</title>
</head>
<body>
<form name="acsForm" action="" method="post" >
<textarea rows=10 cols=80 name="xmlText"><?xml version="1.0" encoding="UTF-8"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">
</samlp:Response>
</textarea>
<textarea name="2nd"> text2....</textarea>
</form>
</body>
</html>
测试֤
文本2。。。。
我的任务是从HTML中提取第一个textarea
中包含的文本,这是一个XML片段。没有对原始代码段进行任何更改。我可以通过使用BeautifulSoup获得它,但它将所有标记名改为小写 尝试使用BeautifulSoup库的一部分,它是为XML设计的。也许可以,尽管我自己从未使用过它,所以我不知道做你想做的事情有多容易/复杂。(啊!为什么这么多作者似乎认为
内容不需要HTML转义?傻瓜!)
不幸的是,BeautifulSoup 3.1没有应用(不正确但常见的)处理
的浏览器修复。我刚刚尝试了BeautifulSoup 3.0,但它不适用于我:
xml ='<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"></samlp:Resonse>'
print BeautifulSoup.BeautifulStoneSoup(xml)
<samlp:response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xml=''
打印BeautifulSoup.BeautifulStoneSoup(xml)
最后我发现pyparsing是完成任务的最佳武器:
aStart,aEnd=makeHTMLTags(“textarea”)
搜索=aStart+SkipTo(aEnd)(“正文”)+aEnd
saml_resp_str=search.searchString(doc)[0]。正文
relay\u state\u str=search.searchString(doc)[1]。正文