suzukiyou blog

建築設計とかpythonとかその辺をとりあえずする

pythonで国立国会図書館のAPIを叩いて保存する

さっそくやっていきます。

スクリプトする。

#coding:utf-8
#samp.py

import urllib2
import codecs

if __name__=="__main__":

    oqfp=codecs.open("query.txt","w","utf-8")
    orfp=codecs.open("log.xml","w","utf-8")
    
    ndlurl=r"http://iss.ndl.go.jp/api/sru?operation=searchRetrieve&version=1.2&recordSchema=dcndl&onlyBib=true&recordPacking=xml"

    isbn="4876980438"
    query="".join([ndlurl,r"&query=isbn%3d%22",isbn,r"%22","%20AND%20dpid%3diss-ndl-opac"])

    oqfp.write(unicode(query))

    response=urllib2.urlopen(query)
    source=response.read()

    dec=codecs.getdecoder("utf-8")
    rdf=dec(source)[0].replace(u"&lt;",u"<").replace(u"&gt;",u">").replace(u"&quot;",u"\"")

    orfp.write(rdf)
    

叩く。

python samp.py

query.txtはこのような感じになる。

http://iss.ndl.go.jp/api/sru?operation=searchRetrieve&version=1.2&recordSchema=dcndl&onlyBib=true&recordPacking=xml&query=isbn%3d%224876980438%22%20AND%20dpid%3diss-ndl-opac

log.xmlrdf-xml形式でこう。

<?xml version="1.0" encoding="UTF-8"?>
<searchRetrieveResponse xmlns="http://www.loc.gov/zing/srw/">
  <version>1.2</version>
  <numberOfRecords>1</numberOfRecords>
  <nextRecordPosition>0</nextRecordPosition>
  <extraResponseData>
<facets>
  <lst name="REPOSITORY_NO">
    <int name="R100000001">1</int>
    <int name="R100000002">1</int>
  </lst>
  <lst name="NDC">
    <int name="5">1</int>
  </lst>
  <lst name="ISSUED_DATE">
    <int name="1997">1</int>
  </lst>
  <lst name="LIBRARY">
    <int name="佐賀県立図書館">1</int>
    <int name="名古屋市鶴舞中央図書館">1</int>
    <int name="国立国会図書館">1</int>
    <int name="大阪市立図書館">1</int>
    <int name="大阪府立中央図書館">1</int>
    <int name="岐阜県図書館">1</int>
    <int name="岡山県立図書館">1</int>
    <int name="徳島県立図書館">1</int>
    <int name="愛知県図書館">1</int>
    <int name="札幌市中央図書館">1</int>
    <int name="東京都立中央図書館">1</int>
    <int name="滋賀県立図書館">1</int>
    <int name="石川県立図書館">1</int>
    <int name="神奈川県立川崎図書館">1</int>
    <int name="福井県立図書館">1</int>
    <int name="秋田県立図書館">1</int>
    <int name="茨城県立図書館">1</int>
    <int name="香川県立図書館">1</int>
    <int name="鳥取県立図書館">1</int>
  </lst>
</facets>
  </extraResponseData>
  <records>
    <record>
      <recordSchema>info:srw/schema/1/dc-v1.1</recordSchema>
      <recordPacking>xml</recordPacking>
      <recordData>
<rdf:RDF xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:dcndl="http://ndl.go.jp/dcndl/terms/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <dcndl:BibAdminResource rdf:about="http://iss.ndl.go.jp/books/R100000002-I000002639920-00">
    <dcndl:catalogingStatus>C7</dcndl:catalogingStatus>
    <dcndl:bibRecordCategory>R100000002</dcndl:bibRecordCategory>
    <dcndl:record rdf:resource="http://iss.ndl.go.jp/books/R100000002-I000002639920-00#material"/>
  </dcndl:BibAdminResource>
  <dcndl:BibResource rdf:about="http://iss.ndl.go.jp/books/R100000002-I000002639920-00#material">
    <dcterms:identifier rdf:datatype="http://ndl.go.jp/dcndl/terms/JPNO">98048304</dcterms:identifier>
    <dcterms:identifier rdf:datatype="http://ndl.go.jp/dcndl/terms/ISBN">4-87698-043-8</dcterms:identifier>
    <rdfs:seeAlso rdf:resource="http://id.ndl.go.jp/jpno/98048304"/>
    <rdfs:seeAlso rdf:resource="http://iss.ndl.go.jp/isbn/4876980438"/>
    <dcterms:title>耐震構造設計論</dcterms:title>
    <dc:title>
      <rdf:Description>
        <rdf:value>耐震構造設計論</rdf:value>
        <dcndl:transcription>タイシン コウゾウ セッケイ ロン</dcndl:transcription>
      </rdf:Description>
    </dc:title>
    <dcterms:creator>
      <foaf:Agent rdf:about="http://id.ndl.go.jp/auth/entity/00092326">
        <foaf:name>山田, 善一, 1929-</foaf:name>
        <dcndl:transcription>ヤマダ, ヨシカズ</dcndl:transcription>
      </foaf:Agent>
    </dcterms:creator>
    <dc:creator>山田善一 編著</dc:creator>
    <dcterms:publisher>
      <foaf:Agent>
        <foaf:name>京都大学学術出版会</foaf:name>
        <dcndl:location>京都</dcndl:location>
      </foaf:Agent>
    </dcterms:publisher>
    <dcndl:publicationPlace rdf:datatype="http://purl.org/dc/terms/ISO3166">JP</dcndl:publicationPlace>
    <dcterms:date>1997.9</dcterms:date>
    <dcterms:issued rdf:datatype="http://purl.org/dc/terms/W3CDTF">1997</dcterms:issued>
    <dcterms:description>文献あり</dcterms:description>
    <dcterms:subject>
      <rdf:Description rdf:about="http://id.ndl.go.jp/auth/ndlsh/00572563">
        <rdf:value>耐震構造</rdf:value>
      </rdf:Description>
    </dcterms:subject>
    <dcterms:subject rdf:resource="http://id.ndl.go.jp/class/ndlc/NA93"/>
    <dcterms:subject rdf:resource="http://id.ndl.go.jp/class/ndc9/524.91"/>
    <dcterms:language rdf:datatype="http://purl.org/dc/terms/ISO639-2">jpn</dcterms:language>
    <dcterms:extent>420p ; 23cm</dcterms:extent>
    <dcndl:price>5200円</dcndl:price>
    <dcterms:audience>一般</dcterms:audience>
    <dcndl:materialType rdfs:label="図書" rdf:resource="http://ndl.go.jp/ndltype/Book"/>
    <dcterms:accessRights>S01P99U99</dcterms:accessRights>
  </dcndl:BibResource>
</rdf:RDF>
      </recordData>
      <recordPosition>1</recordPosition>
    </record>
  </records>
</searchRetrieveResponse>

適当にxml.etree.ElementTreeなどを使って読むべし。