Python 为什么这么多行数据可以';你不明白吗?

Python 为什么这么多行数据可以';你不明白吗?,python,html,parsing,lxml,Python,Html,Parsing,Lxml,我的代码: import urllib import lxml.html equitydown="http://sc.hkex.com.hk/gb/www.hkex.com.hk/chi/market/sec_tradinfo/stockcode/eisdeqty_c.htm" file=urllib.urlopen(equitydown).read() root=lxml.html.document_fromstring(file) rdata = root.xpath('//t

我的代码:

import urllib
import lxml.html
equitydown="http://sc.hkex.com.hk/gb/www.hkex.com.hk/chi/market/sec_tradinfo/stockcode/eisdeqty_c.htm"   
file=urllib.urlopen(equitydown).read()   
root=lxml.html.document_fromstring(file) 
rdata = root.xpath('//tr[@class="tr_normal" and (.//img)]')
for data in rdata:
    data.getparent().remove(data)
for code in root.xpath('//tr[@class="tr_normal"]/td[position()=1]'):
    print code
您可以看到输出(省略了许多)

它仍然得到错误的输出,请尝试运行它,看看会发生什么?
00328后面的代码无法获取,原因是什么?

请注意,
00329
是第一个带有
img
的代码,因此我认为您的
删除是问题所在。可能会弄乱xpath迭代器,请尝试先将其转换为
列表

或者尝试:

root=lxml.html.document_fromstring(file)
for code in root.xpath('//tr[@class="tr_normal" and not(.//img)]/td[position()=1]'):
  print code
分流注意事项:

将url内容的副本保存到文件中,以防它是移动目标

把它扔到。。。结果是:

很抱歉,我无法验证此文档,因为第18行显示了它 包含一个或多个无法解释为gb2312的字节(在 换句话说,找到的字节在指定的 字符编码)。请检查文件内容和 字符编码指示

错误是:euc cn“\xFA”未映射到Unicode

旁白:有谎言、该死的谎言和编码声明声称
iso-8859-1
gb2312

试图对内容进行解码('gb2312'
),失败

 >>> guff = open('lxml_hke_raw.htm', 'rb').read()
 >>> len(guff) 715608
 >>> guff.decode('gb2312') Traceback (most recent call last):
 File "<stdin>", line 1, in <module> UnicodeDecodeError: 'gb2312' codec can't
 decode bytes in position 171039-171040: illegal multibyte sequence
 >>> pos=171039
 >>> guff[pos:pos+2]
 '\xfa\xe2'
这可以写入新文件,也可以进行解析:

root = lxml.html.document_fromstring(content2) 
for el in root.iter('tr'):
    if el.get('class') != 'tr_normal': continue
    print all(ch.tag == 'td' for ch in el), [ch.text for ch in el]
缩略输出:

True ['00001', None, '1,000', '#', 'H', 'O', 'F']
True ['00002', None, '500', '#', 'H', 'O', 'F']
...
True ['00328', None, '2,000', '#', u'\xa0', u'\xa0', u'\xa0']
True ['00329', None, '10,000', '#', u'\xa0', u'\xa0', u'\xa0']
True ['00330', None, '100', '#', 'H', 'O', 'F']
...
True ['06880', None, '2,000', '#', u'\xa0', u'\xa0', u'\xa0']
True ['06883', None, '300', '#', u'\xa0', u'\xa0', u'\xa0']
另一个难题:

Python 2.7.2使用
gb18030
对原始内容进行了正常解码。但是更改文件中的字符集无效(在
00329
之后没有输出)。还尝试使用lxml的
编码
arg覆盖编码,具有相同的效果


观察:有问题的
\xfa\xe2
gb18030
解码为
u'\ue331'
,它位于BMP中,用于解释
img
元素,该元素为GIF提供一个url,该GIF将显示预期的字符。

您确定输出中未显示的元素具有正常的类
tr\u
?你试过我的建议了吗?结果如何?
 >>> guff = open('lxml_hke_raw.htm', 'rb').read()
 >>> len(guff) 715608
 >>> guff.decode('gb2312') Traceback (most recent call last):
 File "<stdin>", line 1, in <module> UnicodeDecodeError: 'gb2312' codec can't
 decode bytes in position 171039-171040: illegal multibyte sequence
 >>> pos=171039
 >>> guff[pos:pos+2]
 '\xfa\xe2'
<tr class="tr_normal">
    <td class="verd_black12" width="18%">00329</td>
    <td class="verd_black12" width="42%">
        <a 
            href="http://sc.hkex.com.hk/etc/etc/etc"
            target="_parent"
        >
            <img
                src="http://sc.hkex.com.hk:80/fs?FAE2+5+13+004B96"
                alt="\xfa\xe2"  #### not a valid gb2312 sequence ####
            > #### also the "img" element is not terminated ####
        \xc1\xfa\xb9\xfa\xbc\xca
        </a>
    </td>
    <td class="verd_black12" width="19%">10,000</td>
    <td class="verd_black12" width="3%" align="center">#</td>
    <td class="verd_black12" width="3%">&nbsp;</td>
    <td class="verd_black12" width="3%">&nbsp;</td>
    <td class="verd_black12" width="3%">&nbsp;</td>
</tr>
ucontent = content.decode('gb2312', 'replace')
repchar = u'\uFFFD'
print ucontent.count(repchar) # 2
ucontent2 = ucontent.replace(repchar, '[NON-GB2312 SEQUENCE]')
content2 = ucontent2.encode('gb2312')
root = lxml.html.document_fromstring(content2) 
for el in root.iter('tr'):
    if el.get('class') != 'tr_normal': continue
    print all(ch.tag == 'td' for ch in el), [ch.text for ch in el]
True ['00001', None, '1,000', '#', 'H', 'O', 'F']
True ['00002', None, '500', '#', 'H', 'O', 'F']
...
True ['00328', None, '2,000', '#', u'\xa0', u'\xa0', u'\xa0']
True ['00329', None, '10,000', '#', u'\xa0', u'\xa0', u'\xa0']
True ['00330', None, '100', '#', 'H', 'O', 'F']
...
True ['06880', None, '2,000', '#', u'\xa0', u'\xa0', u'\xa0']
True ['06883', None, '300', '#', u'\xa0', u'\xa0', u'\xa0']