2011-06-21 4 views
4

次のURL:Israeli Bureau of Statistics Webクエリツールからデータをスクラップする方法はありますか?

http://www.cbs.gov.il/ts/ID40d250e0710c2f/databank/series_func_e_v1.html?level_1=31&level_2=1&level_3=7

は、一度に50系列の最大値に抽出されたデータポイントの数を制限イスラエル政府からの情報のデータ生成を与えます。私は、各ステップのクリックに従うことができるウェブスクレイパーをあなたの好きな言語/ソフトウェアで書くことが可能かどうか(もしそうなら、どのように)、特定のトピックのシリーズをすべて得ることができるのだろうか。

ありがとうございました。

+3

あなたが言語を選んで、それを学ぶようになってきました。あなたが既にpython、perl、およびrを知っていない限り、誰かが特定の言語で可能であると答えると、ソリューションを実装するその次のステップをどのように取るでしょうか?あなたが言及した言語のいずれかがあなたの仕事を処理します。ソリューションを実装するのに十分なことを知っているのはどちらですか? – DavidO

+0

私はRを十分に知っています。しかし、私はRがそれを扱うことができるかどうかは分かりませんでしたので、可能ならば他の言語の人々に質問したり、それを行う際に根本的な問題があるかどうかを知りたかったのです。 –

答えて

3

:導入のための '一目でScrapy' を参照してください

pprint.pprint(browser.form.controls[-2].items) 
# [<Item name='1' id=None selected='selected' contents='Volume of orders for the domestic market' value='1' label='Volume of orders for the domestic market'>, 
# <Item name='2' id=None contents='Orders for export' value='2' label='Orders for export'>, 
# <Item name='3' id=None contents='The volume of production' value='3' label='The volume of production'>, 
# <Item name='4' id=None contents='The volume of sales' value='4' label='The volume of sales'>, 
# <Item name='5' id=None contents='Stocks of finished goods' value='5' label='Stocks of finished goods'>, 
# <Item name='6' id=None contents='Access to credit for the company' value='6' label='Access to credit for the company'>, 
# <Item name='7' id=None contents='Change in the number of employees' value='7' label='Change in the number of employees'>] 

choices=[item.attrs['value'] for item in browser.form.controls[-2].items] 
print(choices) 
# ['1', '2', '3', '4', '5', '6', '7'] 

browser.form['name_tatser']=['2'] 
browser.submit() 

たちができる:ここでは

import mechanize 
import pprint 
import lxml.etree as ET 
import lxml.html as lh 
import urllib 
import urllib2 

browser=mechanize.Browser() 
browser.open("http://www.cbs.gov.il/ts/ID40d250e0710c2f/databank/series_func_e_v1.html?level_1=31&level_2=1&level_3=7") 
browser.select_form(nr=0) 

我々は利用可能なオプションを覗きますそれ以降のフォームごとにこれを繰り返します。

browser.select_form(nr=1) 

choices=[item.attrs['value'] for item in browser.form.controls[-2].items] 
print(choices) 
# ['1576', '1581', '1594', '1595', '1596', '1598', '1597', '1593'] 

browser.form['name_ser']=['1576'] 
browser.submit() 

browser.select_form(nr=2) 

choices=[item.attrs['value'] for item in browser.form.controls[-2].items] 
print(choices) 
# ['32', '33', '34', '35', '36', '37', '38', '39', '40', '41'] 

browser.form['data_kind']=['33'] 
browser.submit() 

browser.select_form(nr=3) 
browser.form['ybegin']=['2010'] 
browser.form['mbegin']=['1'] 
browser.form['yend']=['2011'] 
browser.form['mend']=['5'] 
browser.submit() 

この時点で、次の3つのオプションがあります。

  1. を私はどんな経験を持っていない

XMLファイルをダウンロードし

  • .xlsファイルをダウンロードしてHTMLソース
  • からデータを解析Pythonで.xlsを解析するので、私はこのオプションを渡しました。

    BeautifulSoupまたはlxmlでHTMLを解析することができます。おそらく これは最短の解決策でしたが、HTMLのための適切なXPathを見つけることはすぐにわかりませんでしたので、私はXMLのために行きました:

    XMLをcbs.gov.ilのWebサイトからダウンロードするには、 javascript関数を呼び出すボタンをクリックします。ああ、機械化はjavascript関数を実行できません。ありがたいことに、javascriptは新しいURLをアセンブルするだけです。 lxmlでパラメータを引き出すことは簡単です:

    content=browser.response().read() 
    doc=lh.fromstring(content) 
    params=dict((elt.attrib['name'],elt.attrib['value']) for elt in doc.xpath('//input')) 
    params['king_format']=2 
    url='http://www.cbs.gov.il/ts/databank/data_ts_format_e.xml' 
    params=urllib.urlencode(dict((p,params[p]) for p in [ 
        'king_format', 
        'tod', 
        'time_unit_list', 
        'mend', 
        'yend', 
        'co_code_list', 
        'name_tatser_list', 
        'ybegin', 
        'mbegin', 
        'code_list', 
        'co_name_tatser_list', 
        'level_1', 
        'level_2', 
        'level_3'])) 
    
    browser.open(url+'?'+params) 
    content=browser.response().read() 
    

    は、今、私たちは別のつまずきに達する:XMLはiso-8859-8-iでエンコードされます。 Pythonはこのエンコーディングを認識しません。何をすべきか分からず、私は単にiso-8859-8-iiso-8859-8に置き換えました。私はこの悪影響がどのような悪影響をもたらすのか分かりません。

    # A hack, since I do not know how to deal with iso-8859-8-i 
    content=content.replace('iso-8859-8-i','iso-8859-8') 
    doc=ET.fromstring(content) 
    

    あなたは遠くこれを取得したら、XMLを解析するのは簡単です:

    for series in doc.xpath('/series_ts/Data_Set/Series'): 
        print(series.attrib) 
        # {'calc_kind': 'Weighted', 
        # 'name_ser': 'Number Of Companies That Answered', 
        # 'get_time': '2011-06-21', 
        # 'name_topic': "Business Tendency Survey - Distributions Of Businesses By Industry, Kind Of Questions And Answers - Manufacturing - Company'S Experience Over The Past Three Months - Orders For Export", 
        # 'time_unit': 'Month', 
        # 'code_series': '22978', 
        # 'data_kind': '5-10 Employed Persons', 
        # 'decimals': '0', 
        # 'unit_kind': 'Number'} 
    
        for elt in series.xpath('obs'): 
         print(elt.attrib) 
         # {'time_period': ' 2010-12', 'value': '40'} 
         # {'time_period': ' 2011-01', 'value': '38'} 
         # {'time_period': ' 2011-02', 'value': '40'} 
         # {'time_period': ' 2011-03', 'value': '36'} 
         # {'time_period': ' 2011-04', 'value': '30'} 
         # {'time_period': ' 2011-05', 'value': '33'} 
    
  • +0

    大丈夫、これは非常に印象的です、ありがとう! –

    8

    WWW::MechanizeWWW::HtmlUnitをご覧ください。

    #!/usr/bin/perl 
    
    use strict; 
    use warnings; 
    
    use WWW::Mechanize; 
    
    my $m = WWW::Mechanize->new; 
    
    #get page 
    $m->get("http://www.cbs.gov.il/ts/ID40d250e0710c2f/databank/series_func_e_v1.html?level_1=31&level_2=1&level_3=7"); 
    
    #submit the form on the first page 
    $m->submit_form(
        with_fields => { 
         name_tatser => 2, #Orders for export 
        } 
    ); 
    
    #now that we have the second page, submit the form on it 
    $m->submit_form(
        with_fields => { 
         name_ser => 1576, #Number of companies that answered 
        } 
    ); 
    
    #and so on... 
    
    #printing the source HTML is a good way 
    #to find out what you need to do next 
    print $m->content; 
    
    +0

    うわー。印象的な - ありがとう! –

    関連する問題