Python: 'NoneType'对象没有属性'childGenerator'

时间:2021-08-01 09:03:39

My code works perfectly to parse some HTML tables, but it fails on this table with the error 'NoneType' object has no attribute 'childGenerator' and I'm not sure why because it appears identical. Maybe it's the underlying HTML code that messes it up?

我的代码很好地解析了一些HTML表,但是在这个表上它失败了,错误的“NoneType”对象没有属性“childGenerator”,我不确定为什么它看起来是一样的。也许是底层的HTML代码把它搞砸了?

Here's a table I can successfully parse:

这是一个我可以成功解析的表:

Python: 'NoneType'对象没有属性'childGenerator'

and it's HTML code:

HTML代码:

<html>
div.WordSection1
    {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='color:#1F497D'>Sector 2 has updated its quota listings.<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>Please see below.<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>Thanks,<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><div><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Franklin Gothic Book","sans-serif";color:#1F497D'>Claire Fitz-Gerald<o:p></o:p></span></p><p class=MsoNormal><i><span style='font-size:10.0pt;font-family:"Franklin Gothic Book","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></i></p><p class=MsoNormal><b><span style='font-family:"Franklin Gothic Demi","sans-serif";color:#002776'>Cape Cod Commercial Fishermen's Alliance<o:p></o:p></span></b></p><p class=MsoNormal><b><span style='font-family:"Franklin Gothic Book","sans-serif";color:#DE3500'>~ Small Boats.&nbsp; Big Ideas. ~</span></b><b><span style='color:#DE3500'><o:p></o:p></span></b></p></div><p class=MsoNormal><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> David Leveille [mailto:nefs02@gmail.com] <br><b>Sent:</b> Wednesday, May 21, 2014 2:00 PM<br><b>To:</b> David Leveille<br><b>Subject:</b> Corrected NEFS 2 Available Quota 5/21<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Arial","sans-serif";color:#1F487E'>AVAILABLE QUOTA FY 2014</span><span style='font-size:12.0pt;font-family:"Times New Roman","serif"'><o:p></o:p></span></p><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width="75%" style='width:75.3%'><tr><td width=276 style='width:207.35pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><b><span style='font-size:9.0pt;font-family:"Arial","sans-serif";color:black'>ID <o:p></o:p></span></b></p></td><td width=202 style='width:151.75pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='mso-line-height-alt:15.0pt'><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Available Quota <o:p></o:p></span></b></p></td><td width=119 style='width:89.55pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='mso-line-height-alt:15.0pt'><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Live Weight Pounds <o:p></o:p></span></b></p></td><td width=182 style='width:136.65pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='mso-line-height-alt:15.0pt'><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Price <o:p></o:p></span></b></p></td><td width=108 style='width:81.05pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='mso-line-height-alt:15.0pt'><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Date Posted <o:p></o:p></span></b></p></td></tr><tr><td width=276 style='width:207.35pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>1724<o:p></o:p></span></p></td><td width=202 style='width:151.75pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM COD<br>GOM HADD<br>GOM BB<br>GREYSOLE<br>DABS<br>GOM YT<o:p></o:p></span></p></td><td width=119 style='width:89.55pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>2328<br>445<br>3007<br>850<br>3101<br>1995<o:p></o:p></span></p></td><td width=182 style='width:136.65pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$9,000<o:p></o:p></span></p></td><td width=108 style='width:81.05pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>5/20<o:p></o:p></span></p></td></tr><tr><td width=276 style='width:207.35pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>1578<o:p></o:p></span></p></td><td width=202 style='width:151.75pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GBE COD<br>GB BB<br>GB YT<br>SNE BB<br>SNE YT<br>GOM BB<br>Whake<br>POLL<br>RED<o:p></o:p></span></p></td><td width=119 style='width:89.55pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>538<br>1755<br>243<br>490<br>153<br>3965<br>2727<br>9227<br>15060<o:p></o:p></span></p></td><td width=182 style='width:136.65pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$1.00<br>$0.20<br>$1.00<br>$0.45<br>$0.50<br>$0.15<br>$0.20<br>$0.01<br>$0.01<o:p></o:p></span></p></td><td width=108 style='width:81.05pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>5/20<o:p></o:p></span></p></td></tr><tr><td width=276 style='width:207.35pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>310<o:p></o:p></span></p></td><td width=202 style='width:151.75pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GBE COD<br>GBW COD<br>DABS<br>WHAKE<br>POLL<br>RED<br>SNE BB<br>GOM BB<o:p></o:p></span></p></td><td width=119 style='width:89.55pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>825<br>9033<br>12419<br>3120<br>65234<br>76610<br>2121<br>7285<o:p></o:p></span></p></td><td width=182 style='width:136.65pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>15,000<o:p></o:p></span></p></td><td width=108 style='width:81.05pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>5/20<o:p></o:p></span></p></td></tr><tr style='height:23.25pt'><td width=276 style='width:207.35pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt;height:23.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>347<o:p></o:p></span></p></td><td width=202 style='width:151.75pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt;height:23.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>SNE BB<o:p></o:p></span></p></td><td width=119 style='width:89.55pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt;height:23.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>8,000<o:p></o:p></span></p></td><td width=182 style='width:136.65pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt;height:23.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$0.50<o:p></o:p></span></p></td><td width=108 style='width:81.05pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt;height:23.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>5/7<o:p></o:p></span></p></td></tr><tr><td width=276 style='width:207.35pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>1878A<o:p></o:p></span></p></td><td width=202 style='width:151.75pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM COD<br>GOM HADD<br>SNE BB<br>GOM BB<br>GB BB<br>GREYSOLE<br>GOM YT<br>SNE YT<br>POLL<o:p></o:p></span></p></td><td width=119 style='width:89.55pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>6188<br>635<br>3916<br>7873<br>6762<br>3358<br>9776<br>271<br>186550<o:p></o:p></span></p></td><td width=182 style='width:136.65pt;border:solid windowtext 1.0pt;background:white;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal style='line-height:15.0pt'><span style='font-size:13.5pt;font-family:"Arial","sans-serif";

And here's the table that I fail at parsing for reasons I don't know:

这是我在解析中失败的表格我不知道的原因是:

Python: 'NoneType'对象没有属性'childGenerator'

and its corresponding HTML code:

及其对应的HTML代码:

<html>
div.WordSection1
    {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='color:#1F497D'>Please see quota listings below.<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><div><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Franklin Gothic Book","sans-serif";color:#1F497D'>Claire Fitz-Gerald<o:p></o:p></span></p><p class=MsoNormal><i><span style='font-size:10.0pt;font-family:"Franklin Gothic Book","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></i></p><p class=MsoNormal><b><span style='font-family:"Franklin Gothic Demi","sans-serif";color:#002776'>Cape Cod Commercial Fishermen's Alliance<o:p></o:p></span></b></p><p class=MsoNormal><b><span style='font-family:"Franklin Gothic Book","sans-serif";color:#DE3500'>~ Small Boats.&nbsp; Big Ideas. ~</span></b><b><span style='color:#DE3500'><o:p></o:p></span></b></p></div><p class=MsoNormal><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> David Leveille [mailto:nefs02@gmail.com] <br><b>Sent:</b> Wednesday, November 06, 2013 1:46 PM<br><b>To:</b> David Leveille<br><b>Subject:</b> NEFS 2 Available quota 11/6<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><span style='font-size:16.5pt;font-family:"Arial","sans-serif";color:#1F487E'>AVAILABLE QUOTA 2013</span><span style='font-size:12.0pt;font-family:"Times New Roman","serif"'> <o:p></o:p></span></p><table class=MsoNormalTable border=1 cellspacing=0 cellpadding=0 width="77%" style='width:77.28%;border:none;border-top:solid windowtext 1.0pt'><tr><td width=66 style='width:49.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:9.0pt;font-family:"Arial","sans-serif";color:black'>ID <o:p></o:p></span></b></p></td><td width=162 style='width:121.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Available Quota <o:p></o:p></span></b></p></td><td width=330 style='width:247.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Live Weight Pounds <o:p></o:p></span></b></p></td><td width=145 style='width:108.75pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Price <o:p></o:p></span></b></p></td><td width=86 style='width:64.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Date Posted <o:p></o:p></span></b></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>1767<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM COD<br>GOM HADD<br>DABS<br>GOM WINTER<br>GOM YELLOWTAIL<br>WHITE HAKE<br>GREYSOLE<br>POLLOCK<br>REDS<o:p></o:p></span></p></td><td width=330 style='width:247.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>8,334<br>1,586<br>1,494<br>11,722<br>6,191<br>476<br>959<br>3,886<br>8,825<o:p></o:p></span></p></td><td width=145 style='width:108.75pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$1.50<br>$1.30<br>$0.45<br>$0.15<br>$0.90<br>$0.30<br>$1.00<br>$0.01<br>$0.01<o:p></o:p></span></p></td><td width=86 style='width:64.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>11/4<o:p></o:p></span></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>001<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM HADD<o:p></o:p></span></p></td><td width=330 style='width:247.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>859<o:p></o:p></span></p></td><td width=145 style='width:108.75pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$1.25<o:p></o:p></span></p></td><td width=86 style='width:64.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>10/30<o:p></o:p></span></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>1153<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM YellowTail<br>SNE Winter<o:p></o:p></span></p></td><td width=330 style='width:247.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>6,000<br>8,000<o:p></o:p></span></p></td><td width=145 style='width:108.75pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$0.80<br>$0.45<o:p></o:p></span></p></td><td width=86 style='width:64.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>10/29<o:p></o:p></span></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>160<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";

And here's my relevant code:

这是我的相关代码:

def celltext(cell):
    textlist=[]
    y = cell.find('span')
    for a in y.childGenerator(): 
        if isinstance(a, NavigableString):
            textlist.append(str(a))
    return (textlist)

for row in table.find_all('tr'):    
    columns = row.find_all('td')
    try:
        if columns[0].get_text().strip()!='ID':# skip header
            print("First Column:", columns[0].get_text().strip())
            quota = columns[1].get_text().strip()
            print(quota)
            Quota = celltext(columns[1]) 
            Weight =  celltext(columns[2])

The code fails on the line Quota = celltext(columns[1]). I know the NoneType error is a result of a function working on an object or value that is None but I can't understand why my code runs perfectly on several HTML tables but fails miserably on this one.

代码在行配额= celltext(列[1])上失败。我知道非etype错误是一个函数在一个对象或值为None上工作的结果,但是我不明白为什么我的代码在几个HTML表上运行得很好,但是在这个表上却失败得很惨。

Any help explaining this would be appreciated, thanks.

如果您能帮忙解释一下,我将不胜感激,谢谢。

2 个解决方案

#1


0  

This error happens on the 3rd line of the celltext function:

这个错误发生在celltext函数的第3行:

y = cell.find('span')
for a in y.childGenerator():   <--

Most likely, y is None because 'span' was not found. Print what is in cell. If there is no 'span' there, you will want to add a condition in celltext.

很有可能,y不是因为“span”没有被找到。打印在单元格中。如果没有“span”,您将希望在celltext中添加一个条件。

#2


0  

I copied an pasted your code with the content of the second table and it worked fine:

我用第二张表的内容复制粘贴了你的代码,效果很好:

from bs4 import BeautifulSoup

html = """<html>
div.WordSection1
    {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='color:#1F497D'>Please see quota listings below.<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><div><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Franklin Gothic Book","sans-serif";color:#1F497D'>Claire Fitz-Gerald<o:p></o:p></span></p><p class=MsoNormal><i><span style='font-size:10.0pt;font-family:"Franklin Gothic Book","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></i></p><p class=MsoNormal><b><span style='font-family:"Franklin Gothic Demi","sans-serif";color:#002776'>Cape Cod Commercial Fishermen's Alliance<o:p></o:p></span></b></p><p class=MsoNormal><b><span style='font-family:"Franklin Gothic Book","sans-serif";color:#DE3500'>~ Small Boats.&nbsp; Big Ideas. ~</span></b><b><span style='color:#DE3500'><o:p></o:p></span></b></p></div><p class=MsoNormal><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> David Leveille [mailto:nefs02@gmail.com] <br><b>Sent:</b> Wednesday, November 06, 2013 1:46 PM<br><b>To:</b> David Leveille<br><b>Subject:</b> NEFS 2 Available quota 11/6<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><span style='font-size:16.5pt;font-family:"Arial","sans-serif";color:#1F487E'>AVAILABLE QUOTA 2013</span><span style='font-size:12.0pt;font-family:"Times New Roman","serif"'> <o:p></o:p></span></p><table class=MsoNormalTable border=1 cellspacing=0 cellpadding=0 width="77%" style='width:77.28%;border:none;border-top:solid windowtext 1.0pt'><tr><td width=66 style='width:49.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:9.0pt;font-family:"Arial","sans-serif";color:black'>ID <o:p></o:p></span></b></p></td><td width=162 style='width:121.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Available Quota <o:p></o:p></span></b></p></td><td width=330 style='width:247.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Live Weight Pounds <o:p></o:p></span></b></p></td><td width=145 style='width:108.75pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Price <o:p></o:p></span></b></p></td><td width=86 style='width:64.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Date Posted <o:p></o:p></span></b></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>1767<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM COD<br>GOM HADD<br>DABS<br>GOM WINTER<br>GOM YELLOWTAIL<br>WHITE HAKE<br>GREYSOLE<br>POLLOCK<br>REDS<o:p></o:p></span></p></td><td width=330 style='width:247.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>8,334<br>1,586<br>1,494<br>11,722<br>6,191<br>476<br>959<br>3,886<br>8,825<o:p></o:p></span></p></td><td width=145 style='width:108.75pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$1.50<br>$1.30<br>$0.45<br>$0.15<br>$0.90<br>$0.30<br>$1.00<br>$0.01<br>$0.01<o:p></o:p></span></p></td><td width=86 style='width:64.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>11/4<o:p></o:p></span></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>001<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM HADD<o:p></o:p></span></p></td><td width=330 style='width:247.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>859<o:p></o:p></span></p></td><td width=145 style='width:108.75pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$1.25<o:p></o:p></span></p></td><td width=86 style='width:64.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>10/30<o:p></o:p></span></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>1153<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM YellowTail<br>SNE Winter<o:p></o:p></span></p></td><td width=330 style='width:247.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>6,000<br>8,000<o:p></o:p></span></p></td><td width=145 style='width:108.75pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$0.80<br>$0.45<o:p></o:p></span></p></td><td width=86 style='width:64.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>10/29<o:p></o:p></span></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>160<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";"""

table = BeautifulSoup(html, 'html.parser')

def celltext(cell):
    textlist=[]
    y = cell.find('span')
    for a in y.childGenerator(): 
        if isinstance(a, NavigableString):
            textlist.append(str(a))
    return (textlist)

for row in table.find_all('tr'):    
    columns = row.find_all('td')
    try:
        if columns[0].get_text().strip()!='ID':# skip header
            print("First Column:", columns[0].get_text().strip())
            quota = columns[1].get_text().strip()
            print(quota)
            Quota = celltext(columns[1]) 
            Weight =  celltext(columns[2])
    except:
        print("Error on this row?")  # you may want to print the row content

First Column: 1767
GOM CODGOM HADDDABSGOM WINTERGOM YELLOWTAILWHITE HAKEGREYSOLEPOLLOCKREDS
Error on this row?
First Column: 001
GOM HADD
Error on this row?
First Column: 1153
GOM YellowTailSNE Winter
Error on this row?
First Column: 160

第一列:1767 GOM CODGOM HADDDABSGOM WINTERGOM yellow tailtail white hakegreypolsolepollockred这一列有错误?第一列:001 GOM HADD错误?第一列:1153 GOM YellowTailSNE冬季错误?第一列:160

Error on this row?

在这一行错误吗?

But in any case, the script will end with an error if there is no 'span' to be found because cell.find('span') will be None, so it cannot have a childGenerator attribute. You may want to add an exception in case there is no 'span'.

但是在任何情况下,如果没有找到“span”,脚本就会以一个错误结束,因为cell.find(“span”)将是None,因此它不能有一个childGenerator属性。如果没有“span”,您可能需要添加一个异常。

The problem may come from the fact that you are running the same script twice in the same session and variables could be overridden unexpectedly. Try to put your main loop in a function (with the html text as parameter) instead.

问题可能来自这样一个事实:您正在同一会话中运行同一脚本两次,并且可以意外地覆盖变量。尝试将主循环放在函数中(以html文本作为参数)。

#1


0  

This error happens on the 3rd line of the celltext function:

这个错误发生在celltext函数的第3行:

y = cell.find('span')
for a in y.childGenerator():   <--

Most likely, y is None because 'span' was not found. Print what is in cell. If there is no 'span' there, you will want to add a condition in celltext.

很有可能,y不是因为“span”没有被找到。打印在单元格中。如果没有“span”,您将希望在celltext中添加一个条件。

#2


0  

I copied an pasted your code with the content of the second table and it worked fine:

我用第二张表的内容复制粘贴了你的代码,效果很好:

from bs4 import BeautifulSoup

html = """<html>
div.WordSection1
    {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='color:#1F497D'>Please see quota listings below.<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><div><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Franklin Gothic Book","sans-serif";color:#1F497D'>Claire Fitz-Gerald<o:p></o:p></span></p><p class=MsoNormal><i><span style='font-size:10.0pt;font-family:"Franklin Gothic Book","sans-serif";color:#1F497D'><o:p>&nbsp;</o:p></span></i></p><p class=MsoNormal><b><span style='font-family:"Franklin Gothic Demi","sans-serif";color:#002776'>Cape Cod Commercial Fishermen's Alliance<o:p></o:p></span></b></p><p class=MsoNormal><b><span style='font-family:"Franklin Gothic Book","sans-serif";color:#DE3500'>~ Small Boats.&nbsp; Big Ideas. ~</span></b><b><span style='color:#DE3500'><o:p></o:p></span></b></p></div><p class=MsoNormal><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><div><div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> David Leveille [mailto:nefs02@gmail.com] <br><b>Sent:</b> Wednesday, November 06, 2013 1:46 PM<br><b>To:</b> David Leveille<br><b>Subject:</b> NEFS 2 Available quota 11/6<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><span style='font-size:16.5pt;font-family:"Arial","sans-serif";color:#1F487E'>AVAILABLE QUOTA 2013</span><span style='font-size:12.0pt;font-family:"Times New Roman","serif"'> <o:p></o:p></span></p><table class=MsoNormalTable border=1 cellspacing=0 cellpadding=0 width="77%" style='width:77.28%;border:none;border-top:solid windowtext 1.0pt'><tr><td width=66 style='width:49.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:9.0pt;font-family:"Arial","sans-serif";color:black'>ID <o:p></o:p></span></b></p></td><td width=162 style='width:121.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Available Quota <o:p></o:p></span></b></p></td><td width=330 style='width:247.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Live Weight Pounds <o:p></o:p></span></b></p></td><td width=145 style='width:108.75pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Price <o:p></o:p></span></b></p></td><td width=86 style='width:64.5pt;border:none;border-bottom:solid windowtext 1.0pt;background:#8BCDFF;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:black'>Date Posted <o:p></o:p></span></b></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>1767<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM COD<br>GOM HADD<br>DABS<br>GOM WINTER<br>GOM YELLOWTAIL<br>WHITE HAKE<br>GREYSOLE<br>POLLOCK<br>REDS<o:p></o:p></span></p></td><td width=330 style='width:247.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>8,334<br>1,586<br>1,494<br>11,722<br>6,191<br>476<br>959<br>3,886<br>8,825<o:p></o:p></span></p></td><td width=145 style='width:108.75pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$1.50<br>$1.30<br>$0.45<br>$0.15<br>$0.90<br>$0.30<br>$1.00<br>$0.01<br>$0.01<o:p></o:p></span></p></td><td width=86 style='width:64.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>11/4<o:p></o:p></span></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>001<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM HADD<o:p></o:p></span></p></td><td width=330 style='width:247.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>859<o:p></o:p></span></p></td><td width=145 style='width:108.75pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$1.25<o:p></o:p></span></p></td><td width=86 style='width:64.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>10/30<o:p></o:p></span></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>1153<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>GOM YellowTail<br>SNE Winter<o:p></o:p></span></p></td><td width=330 style='width:247.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>6,000<br>8,000<o:p></o:p></span></p></td><td width=145 style='width:108.75pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>$0.80<br>$0.45<o:p></o:p></span></p></td><td width=86 style='width:64.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>10/29<o:p></o:p></span></p></td></tr><tr><td width=66 style='width:49.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";color:black'>160<o:p></o:p></span></p></td><td width=162 style='width:121.5pt;border:solid windowtext 1.0pt;padding:2.25pt 2.25pt 2.25pt 2.25pt'><p class=MsoNormal><span style='font-size:13.5pt;font-family:"Arial","sans-serif";"""

table = BeautifulSoup(html, 'html.parser')

def celltext(cell):
    textlist=[]
    y = cell.find('span')
    for a in y.childGenerator(): 
        if isinstance(a, NavigableString):
            textlist.append(str(a))
    return (textlist)

for row in table.find_all('tr'):    
    columns = row.find_all('td')
    try:
        if columns[0].get_text().strip()!='ID':# skip header
            print("First Column:", columns[0].get_text().strip())
            quota = columns[1].get_text().strip()
            print(quota)
            Quota = celltext(columns[1]) 
            Weight =  celltext(columns[2])
    except:
        print("Error on this row?")  # you may want to print the row content

First Column: 1767
GOM CODGOM HADDDABSGOM WINTERGOM YELLOWTAILWHITE HAKEGREYSOLEPOLLOCKREDS
Error on this row?
First Column: 001
GOM HADD
Error on this row?
First Column: 1153
GOM YellowTailSNE Winter
Error on this row?
First Column: 160

第一列:1767 GOM CODGOM HADDDABSGOM WINTERGOM yellow tailtail white hakegreypolsolepollockred这一列有错误?第一列:001 GOM HADD错误?第一列:1153 GOM YellowTailSNE冬季错误?第一列:160

Error on this row?

在这一行错误吗?

But in any case, the script will end with an error if there is no 'span' to be found because cell.find('span') will be None, so it cannot have a childGenerator attribute. You may want to add an exception in case there is no 'span'.

但是在任何情况下,如果没有找到“span”,脚本就会以一个错误结束,因为cell.find(“span”)将是None,因此它不能有一个childGenerator属性。如果没有“span”,您可能需要添加一个异常。

The problem may come from the fact that you are running the same script twice in the same session and variables could be overridden unexpectedly. Try to put your main loop in a function (with the html text as parameter) instead.

问题可能来自这样一个事实:您正在同一会话中运行同一脚本两次,并且可以意外地覆盖变量。尝试将主循环放在函数中(以html文本作为参数)。