开发者

Accessing Javascript Button with Mechanize

I'm trying to download the currency data from bloomberg website with mechanize/python. http://www.bloomberg.com/markets/currencies/

I've managed to update the fields to the desired currencies, unfortunately, i'm having trouble "submitting" the convert button.

I don't think br.submit() is working and when I try to use br.click, it says th开发者_StackOverflow中文版at the item is not clickable. Here's the html of the button

<input type="button" value="Convert" class="btn w75" name="button" onclick="calculate(); return false;">

Someone had suggested using br.form.action"" (http://bytes.com/topic/python/answers/764326-using-mechanize-python-navigate-website) but i'm not sure how I would implement it.

So here's the core of the code so far...

br = mechanize.Browser() #initialized with and lets cookiejar handle all the cookies stuff
br.open('http://www.bloomberg.com/markets/currencies/')
br.form['from_tkc']=['USD:CUR']
br.form['to_tkc']=['BGN:CUR']
br.submit() .
print br.response().read()

Unfortunately, the output html does not have the data I need which is the currency calculator and I know for a fact that the buttons are updated. Can anyone point me in the right direction or help me understand that button a bit more.


This is a pure JavaScript form. Nothing is submitted at all so you cannot access/submit it using Mechanize.

<form id="currcalc" onsubmit="calculate(); return false;" action="#">

http://www.bloomberg.com/javascripts/currencycalc_10.js is the JavaScript behind the calculator and you can find the conversion data directly in the HTML source of the page containing the form:

  <script type="text/javascript">
//<![CDATA[
      var test_obj = new Object();
      var price = new Object();
                price['ADP:CUR'] = 122.244;
                price['AED:CUR'] = 3.67295;
                price['AFN:CUR'] = 45.33;
                price['ALL:CUR'] = 102.325;
...

So if you want to "steal" their conversion data simply extract all lines where the price array (yes I know it's technically an object!) gets populated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜