日本語が化けて大変苦労したのでメモ。結論として、XML(またはHTML)を解析する前にunicode関数に通しておく、ということで良いのかな?相変わらず文字コード関連はよく分からない。 from urllib import urlopen from lxml import etree html = urlopen("http://b.hatena.ne.jp") charset = html.headers.getparam('charset') html_data = unicode(html.read(),charset) et = etree.fromstring(html_data, parser=etree.HTMLParser()) title_element = et.xpath("./head/title")[0] title = title_element.text.e
以前、lingrでlxmlをdisっている人を見たとき、その人のペースに巻き込まれて、lxmlってそういうもんだと思い込んでしまいました。ただ、最近lxmlを使い始めて、なかなかちゃんとしていることが分かってきたので、メモを書き溜めて行こうと思います。というか、オフィシャルなドキュメントの写経に近いのですが。 文字列からElementオブジェクトを生成するetree.fromstringを使う。 from lxml import etree el = etree.fromstring("spam") 整形式XMLでない場合etree.parseの第二引数にetree.HTMLParser()を指定。 from lxml import etree el = etree.fromstring("spam", etree.HTMParser()) 非UTF-8でエンコードされたXML(風の文字列
Most of the YDN APIs can provide their output in JSON format, which side-steps the problem of having to parse data out of them; the data arrives already converted in to a useful data structure. If the API you are using does not yet offer JSON output you can take advantage of Python's excellent XML support. Using minidom Using ElementTree Further Reading Using minidom The most widely understood API
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く