0

嗨,我正在抓取 XML 文件。对于 HTML,我使用了 scrapy,对于 XML,我决定使用xml.sax.

以下是示例代码(不要将其视为真实示例)只是为了查看我的疑问:

from xml.sax.handler import ContentHandler
import xml.sax

xmlFilePath = 'users/documents/jobstext.xml'

try:
    parser = xml.sax.make_parser( )
    parser.parse(open(xmlFilePath))

except (xml.sax.SAXParseException), e:
        print "*** PARSER error: %s" % e
        print e,"What is the error actually >>>>"  

以下是XML 代码

<?xml version="1.0" encoding="utf-8"?>
<jobs>
  <reader><![CDATA[Identity Group]]></reader>
  <readerUrl><![CDATA[http://www.example.com]]></readerUrl>

  <job>
    <title><![CDATA[Architect - OT]]></title>
    <category><![CDATA[LTC/SNF]]></category>
    <jobId><![CDATA[139693]]></jobId>
    <specialization><![CDATA[LTC/SNF]]></specialization>
    <positionType><![CDATA[Travel]]></positionType>
    <description><![CDATA[<DIV>OT&nbsp;needed for a SNF in&nbsp;Oregon.&nbsp; Oregon is a dramatic land of many changes. From the rugged Oregon seacoast, the high mountain passes of the country for Travel Allied Professionals and Travel Nurses. Our clients are among the most prestigious healthcare facilities in the country.</DIV>
<DIV>&nbsp;</DIV>
 </description>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><FONT size=3><SPAN style="FONT-FAMILY: Symbol; COLOR: black; mso-ascii-font-family: 'Times New Roman'">�&lt;/SPAN><SPAN style="COLOR: black"><FONT face="Times New Roman"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Position will manage 24 ED Rooms with 24/7 accountability<o:p></o:p></FONT></SPAN></FONT></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><FONT size=3><SPAN style="FONT-FAMILY: Symbol; COLOR: black; mso-ascii-font-family: 'Times New Roman'">�&lt;/SPAN><SPAN style="COLOR: black"><FONT face="Times New Roman"> <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>55 FTEs <o:p></o:p></FONT></SPAN></FONT></P>
  </job>
</jobs>

结果:

*** PARSER error: users/documents/jobstext.xml:13:150: not well-formed <invalid token>
users/documents/jobstext.xml:13:150: not well-formed <invalid token> What is the error actually >>>>

当执行到达<p>标记和索引 150 并显示错误无效令牌时会发生什么?我期待这是因为?标签,因为您可以在上面的错误中看到这一点。

那么任何人都可以让我知道如何解决not well-formed <invalid token>xml解析中的这个错误,

如果我以错误的格式解释,我很抱歉,但希望我能很好地解释这个概念。

编辑代码:

<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial">THE MOST COMPETITIVE RATES IN NM .....<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial">Busy <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:place w:st="on"><st1:PlaceName w:st="on">Acute</st1:PlaceName> <st1:PlaceName w:st="on">Care</st1:PlaceName> <st1:PlaceType w:st="on">Hospital</st1:PlaceType></st1:place> needs Occupational Therapists.&nbsp; Experience with </SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Ortho, Neuro, vestibular balance, aquatic a plus!<SPAN style="COLOR: black">&nbsp; New grads welcome.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Signon Bonus and help with relocation.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>For more details please call or email Carole 800 995 2673 X1329 or <A href="mailto:cs@coremedicalgroup.com"><SPAN style="mso-bidi-font-weight: bold; mso-bidi-font-size: 12.0pt">cs@coremedicalgroup.com</SPAN></A><o:p></o:p></SPAN></SPAN></P>
4

2 回答 2

1

description没有结束标记,并且其中的 CDATA 部分永远不会终止……尽管我希望它在文档末尾而不是在该元素的第三行数据上出错。

于 2012-07-23T12:23:48.123 回答
1

由于问题已经改变......

必须引用 XML 属性。

例如:class=MsoNormal应该是class="MsoNormal"

于 2012-07-23T12:33:19.413 回答