2012-01-19 5 views
0

マイコード:なぜそれほど多くの回線データを取得できないのですか?

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 

あなたは

 
00320 
00321 
00322 
00323 
00325 
00326 
00327 
00328 

(多くは省略)、出力を見ることができますが、あなたはHong Kong Exchanges and Clearing Limited開くときには、このような(多くは省略)など、多くの行、取得:

 
06830 华众控股 2,000 # 
06838 盈利时 2,000 # 
06868 天福 1,000 #  
06880 豪特保健 2,000 # 
06883 新濠博亚娱乐 300 # 

多くのコードが失われます:06830 06838 06868 06880 06883(多くは省略されています)、コードは00329より後であり、すべて失われます。

私はすべてのコードを取得しません、なぜですか?


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) 
for code in root.xpath('//tr[@class="tr_normal" and not(.//img)]/td[position()=1]'): 
    print code.text_content() 

それはまだ間違った出力が得られ、何が起こるかを見るためにそれを実行しようとしてください? 00328の後ろにあるコードは取得できません。理由は何ですか?

+0

リンクが壊れています。壊れています。 – Maciek

+0

あなたの出力に 'tr_normal'というクラスを持っていないことを確かめていますか? – aayoubi

+1

私はまだ提案したことを試しましたか?結果は何でしたか? –

答えて

0

00329imgの最初のものですので、私はあなたのremoveが問題であると考えています。多分、xpathイテレータを駄目にして、まずlistに変換してみてください。

か試してみてください。

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

トリアージノート:

が、それは動く標的だ場合、ファイルへのURLのコンテンツのコピーを保存しました。 W3C HTML validatorでそれ投げ

...結果だった:

ライン18上で は私が に(GB2312として解釈することができない1バイト以上が含まれているため申し訳ありませんが、私はこの文書を検証することができませんがつまり、見つかったバイトは指定された 文字エンコーディングの有効な値ではありません。ファイルの内容と文字エンコードの指示を確認してください。

エラー:EUC-CN "\ XFAは" 別にユニコード

をマップしません:iso-8859-1またはgb2312を主張嘘、嘘のろわれ、エンコードの宣言があります。

試みたのはcontent.decode('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' 

故障位置周辺探査は、(手動でインデント、多くの無関係な属性が削除または略す)以下を得た:

<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> 

00329を含む要素で停止した理由を示しています。ファイルの後半ではこれが別のケースになることに注意してください。

その場しのぎ:

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'] 

もう一つのパズル:

元のc ontentはgb18030でPython 2.7.2で解読されます。しかし、ファイル内の文字セットを変更しても機能しませんでした(00329以降の出力はありません)。また、lxmlのencoding argを使用してエンコードをオーバーライドしようとしても、同じ効果がありました。

観察:問題\xfa\xe2は、意図文字が表示されますGIFのURLを提供img要素を占めてBMP Private Use Area、であるu'\ue331'gb18030によって復号化されます。

関連する問題