def calculate(expr): """Search for a calculation expression in google. Attempts to search google calculator for the result of an expression. Returns a `CalculatorResult` if successful or `None` if it fails. Args: expr: Calculation expression (eg. "cos(25 pi) / 17.4" or "157.3kg in grams") Returns: CalculatorResult object.""" url = _get_search_url(expr) html = get_html_from_dynamic_site(url) bs = BeautifulSoup(html) cr = CalculatorResult() cr.value = _get_to_value(bs) cr.from_value = _get_from_value(bs) cr.unit = _get_to_unit(bs) cr.from_unit = _get_from_unit(bs) cr.expr = _get_expr(bs) cr.result = _get_result(bs) cr.fullstring = _get_fullstring(bs) return cr
def search(self, query, area='com', ncr=False, void=True, time_period=False, sort_by_date=False, first_page=0): for i in range(first_page, first_page + self.pages): url = _get_search_url(query, i, lang=self.lang, area=area, ncr=ncr, time_period=time_period, sort_by_date=sort_by_date) print(f'Search URL: {url}&tbm=nws') html = parse(get_html(url + "&tbm=nws")) links = html.xpath('//div[@id="rso"]/descendant::a/@href') [self.results.append(link) for link in links if link[0] != '/'] # URL leads out of Google
def search(query, pages=1, lang='en', void=True): """Returns a list of GoogleResult. Args: query: String to search in google. pages: Number of pages where results must be taken. Returns: A GoogleResult object.""" results = [] for i in range(pages): url = _get_search_url(query, i, lang=lang) html = get_html(url) if html: soup = BeautifulSoup(html, "html.parser") lis = soup.findAll("div", attrs={"class": "g"}) j = 0 for li in lis: res = GoogleResult() res.page = i res.index = j res.name = _get_name(li) res.link = _get_link(li) res.google_link = _get_google_link(li) res.description = _get_description(li) res.thumb = _get_thumb() res.cached = _get_cached(li) if void is True: if res.description is None: continue results.append(res) j += 1 return results
def test_get_search_url(self): url = _get_search_url("apple", 0, 10, "en", "jp") exp_url = "http://www.google.co.jp/search?q=apple&start=0&num=10&hl=en" self.assertEqual(url, exp_url)
def test_get_search_url(self): url = _get_search_url("apple", 0, 10, "en") exp_url = "http://www.google.com/search?q=apple&start=0&num=10&nl=en" self.assertEqual(url, exp_url)