update .calc to use lxml, fixes #44
This commit is contained in:
parent
a8eea42f14
commit
a3b295b0c0
|
@ -1,5 +1,3 @@
|
||||||
import re
|
|
||||||
|
|
||||||
from util import hook, http
|
from util import hook, http
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,15 +5,13 @@ from util import hook, http
|
||||||
def calc(inp):
|
def calc(inp):
|
||||||
'''.calc <term> -- returns Google Calculator result'''
|
'''.calc <term> -- returns Google Calculator result'''
|
||||||
|
|
||||||
page = http.get('http://www.google.com/search', q=inp)
|
h = http.get_html('http://www.google.com/search', q=inp)
|
||||||
|
|
||||||
# ugh, scraping HTML with regexes
|
m = h.xpath('//h2[@class="r"]/text()')
|
||||||
m = re.search(r'<h2 class=r style="font-size:138%"><b>(.*?)</b>', page)
|
|
||||||
|
|
||||||
if m is None:
|
if not m:
|
||||||
return "could not calculate " + inp
|
return "could not calculate " + inp
|
||||||
|
|
||||||
result = m.group(1).replace("<font size=-2> </font>", ",")
|
res = ' '.join(m[0].split())
|
||||||
result = result.replace(" × 10<sup>", "E").replace("</sup>", "")
|
|
||||||
result = result.replace("\xa0", ",")
|
return res
|
||||||
return result
|
|
||||||
|
|
Loading…
Reference in New Issue