2017-09-25 2 views
1

月と年の入力を指定してMexico's centralからドロップダウン値を抽出しようとしていますが、実行するたびにクロムウィンドウを開きたくないため、私のスクリプト。ここで私はこれまで試したものです:Banxicoのドロップダウンボックスからのインフレーションの解消

ツイル

from twill.commands import * 
go("http://www.banxico.org.mx/portal-inflacion/inflacion.html") 
showforms() 
Output: Form name=_BBM_MenuForm (#1) 
## ## __Name__________________ __Type___ __ID________ 
__Value__________________ 
1  url      hidden url   
/AplBusquedasBM2/bgenwww_in.jsp 
2  appname     hidden appname  bmsearch 
3  _action     hidden _action  search 
4  _lang     hidden _lang  es 
5  _userquery    text  _userquery Buscar... 
6  submit     submit (None)   
7  _P_BM_Deposito   select _P_BM_De ... ['B', 'M', '_', 'W', 'W', 'W', ';', ... 

を私はsubmit入力は、私が探していると思うが、私は表示されませんどのように入力月と年のデータへ。

Mechanizeの

import mechanize 
br = mechanize.Browser() 

b = br.open("http://www.banxico.org.mx/portal-inflacion/inflacion.html") 

b.select_form(nr=0) 

form = br.form 

print(form) 

Output: 
Traceback (most recent call last): 
    File "pago_ext.py", line 16, in <module> 
    b.select_form(nr=0) 
    File "/usr/local/lib/python2.7/dist-packages/mechanize/_response.py", line 106, in __getattr__ 
    return getattr(wrapped, name) 
AttributeError: closeable_response instance has no attribute 'select_form' 

どのように私は、私はHTMLについて多くを知らない考慮のドロップダウンメニューにアクセスすることができます?

答えて

0

RESTful APIとのやり取りは、HTMLからデータを取り除くよりもはるかに簡単です。 Chromes Inspectツールを使用した検査では、Python urllibを使用してRESTリクエストを簡単に複製できました。以下はPython 3で動作します。

'fecha'の日付を簡単に変更できます。また、XML応答から必要なものを抽出する必要があります。

import urllib.request 

headers = {'accion':'dato', 
      'idSeries':'SP30577,SP30578,SP30579,SP74660,SP74661,SP74662,SP74663,SP74664,SP74665', 
      'decimales':'2,2,2,2,2,2,2,2,2', 
      'fecha':'01/02/2017' 
      } 

url = 'http://www.banxico.org.mx/tipcamb/datosieajax' 

data = urllib.parse.urlencode(headers) 
req = urllib.request.Request(url + '?' + data) 
response = urllib.request.urlopen(req).read() 

print(response) 
1

あなたは慎重ページ(ヒント:ネットワークタブ(FirefoxでCTRL + Shift + E)で作られているリクエストを探してください)を検査した場合は、データがjson形式であなたが値を変更するたびに到着したことがわかります(月/年)を入力します。 URLは常に同じで、最後の日付のみが変更されます。いくつかのトリックで、すべてのデータを簡単に取得できます。ここで

があなたのスクレーパーです:

基本的に
import requests 
from collections import OrderedDict 

def get_annual_data(year): 

    data = [] 

    for i in range(1, 13): 
     url = 'http://www.banxico.org.mx/tipcamb/datosieajax?accion=dato&idSeries=SP30577,SP30578,SP30579,SP74660,SP74661,SP74662,SP74663,SP74664,SP74665&decimales=2,2,2,2,2,2,2,2,2&fecha=01/{0}/{1}'.format('0' + str(i) if i < 10 else i, str(year)) 
     x = requests.get(url).json() 
     data.append(x) 

    years_data = OrderedDict() 

    for i, x in enumerate(data): 

     month_data = OrderedDict() 

     month_data['INPC índice general'] = { 
      'Mensual': data[i]['body'][0]['mensaje'], 
      'Acumulada en el año': data[i]['body'][2]['mensaje'], 
      'Anual': data[i]['body'][1]['mensaje'] 
     } 

     month_data['INPC subyacente'] = { 
      'Mensual': data[i]['body'][3]['mensaje'], 
      'Acumulada en el año': data[i]['body'][4]['mensaje'], 
      'Anual': data[i]['body'][5]['mensaje'] 
     } 

     month_data['INPC no subyacente'] = { 
      'Mensual': data[i]['body'][6]['mensaje'], 
      'Acumulada en el año': data[i]['body'][7]['mensaje'], 
      'Anual': data[i]['body'][8]['mensaje'] 
     } 

     years_data[i + 1] = month_data 

    return years_data 

def get_data(start_year, end_year): 

    all_data = OrderedDict() 

    for i in range(start_year, end_year + 1): 
     all_data[i] = get_annual_data(i) 

    return all_data 

、あなたはget_data(start_year, end_year)(例えば、get_data(2010, 2016))とスクレーパーを実行します。情報は、毎年キーとなる素敵なOrderedDictにあり、その年に対応するすべてのデータは値です。構造体の外観(from pprint import pprint; pprint(get_data(2010, 2016)))を見たい場合は、それをきれいに印刷できます。たとえば、次のような値にアクセスすることができます。print(get_data(2014, 2016)[2014][1]['INPC índice general'])これは、次のようになります。{'Mensual': '0.89', 'Acumulada en el año': '0.89', 'Anual': '4.48'}

関連する問題