update .calc to use lxml, fixes #44

This commit is contained in:
Ryan Hitchman 2011-11-07 14:16:56 -06:00
parent a8eea42f14
commit a3b295b0c0
1 changed files with 6 additions and 10 deletions

View File

@ -1,5 +1,3 @@
import re
from util import hook, http
@ -7,15 +5,13 @@ from util import hook, http
def calc(inp):
'''.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 = re.search(r'<h2 class=r style="font-size:138%"><b>(.*?)</b>', page)
m = h.xpath('//h2[@class="r"]/text()')
if m is None:
if not m:
return "could not calculate " + inp
result = m.group(1).replace("<font size=-2> </font>", ",")
result = result.replace(" &#215; 10<sup>", "E").replace("</sup>", "")
result = result.replace("\xa0", ",")
return result
res = ' '.join(m[0].split())
return res