Skip to content

Commit 28d08a8

Browse files
committed
Fix standard search method. Results where "li" tags and now they are "div" tags.
1 parent e6439ff commit 28d08a8

13 files changed

+12947
-22
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ target/
7171
*.egg-info
7272
*.dmp
7373
*.zip
74+
.DS_Store

google/.DS_Store

6 KB
Binary file not shown.

google/modules/calculator.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ def __init__(self):
1414
self.value = None # Result value (eg. 157300.0)
1515
self.from_value = None # Initial value (eg. 157.3)
1616
self.unit = None # Result unit (eg. u'grams') (NOT implemented yet)
17-
self.from_unit = None # Initial unit (eg. u'kilograms') (NOT implemented yet)
18-
self.expr = None # Initial expression (eg. u'157.3 grams') (NOT implemented yet)
19-
self.result = None # Result expression (eg. u'157300 kilograms') (NOT implemented yet)
20-
self.fullstring = None # Complete expression (eg. u'157.3 kilograms = 157300 grams') (NOT implemented yet)
17+
# Initial unit (eg. u'kilograms') (NOT implemented yet)
18+
self.from_unit = None
19+
# Initial expression (eg. u'157.3 grams') (NOT implemented yet)
20+
self.expr = None
21+
# Result expression (eg. u'157300 kilograms') (NOT implemented yet)
22+
self.result = None
23+
# Complete expression (eg. u'157.3 kilograms = 157300 grams') (NOT
24+
# implemented yet)
25+
self.fullstring = None
2126

2227
def __repr__(self):
2328
return unidecode(self.value)

google/modules/standard_search.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ def search(query, pages=1, lang='en', void=True):
6262

6363
if html:
6464
soup = BeautifulSoup(html, "html.parser")
65-
lis = soup.findAll("li", attrs={"class": "g"})
66-
65+
divs = soup.findAll("div", attrs={"class": "g"})
66+
6767
j = 0
68-
for li in lis:
68+
for li in divs:
6969
res = GoogleResult()
7070

7171
res.page = i
@@ -90,7 +90,7 @@ def search(query, pages=1, lang='en', void=True):
9090
def _get_name(li):
9191
"""Return the name of a google search."""
9292
a = li.find("a")
93-
#return a.text.encode("utf-8").strip()
93+
# return a.text.encode("utf-8").strip()
9494
if a is not None:
9595
return a.text.strip()
9696
return None
@@ -136,7 +136,7 @@ def _get_description(li):
136136
if sdiv:
137137
stspan = sdiv.find("span", attrs={"class": "st"})
138138
if stspan is not None:
139-
#return stspan.text.encode("utf-8").strip()
139+
# return stspan.text.encode("utf-8").strip()
140140
return stspan.text.strip()
141141
else:
142142
return None

google/modules/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from selenium import webdriver
55
import urllib2
66
from functools import wraps
7-
#import requests
7+
# import requests
88
from urllib import urlencode
99

1010

google/tests/test_google.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from google import currency, images
55
from mock import Mock
66
import os
7+
import vcr
8+
9+
BASE_DIR = os.path.dirname(__file__)
710

811

912
def load_html_file(path):
@@ -26,6 +29,11 @@ def test_decorated(self):
2629
return test_decorator
2730

2831

32+
# HELPERS
33+
def get_dir_vcr(name):
34+
return os.path.join(BASE_DIR, "vcr_cassetes", name)
35+
36+
2937
class GoogleTest(unittest.TestCase):
3038

3139
@load_html_file("html_files")
@@ -85,14 +93,11 @@ def test_convert_currency(self, html_f):
8593
euros = google.convert_currency(5.0, "USD", "EUR")
8694
self.assertGreater(euros, 0.0)
8795

88-
@load_html_file("html_files")
89-
def test_standard_search(self, html_f):
96+
# @load_html_file("html_files")
97+
@vcr.use_cassette("test_standard_search.yaml")
98+
def test_standard_search(self):
9099
"""Test method to search in google."""
91100

92-
# replace method to get html from a test html file
93-
google.standard_search.get_html = \
94-
Mock(return_value=html_f.read().decode('utf8'))
95-
96101
search = google.search("github")
97102
self.assertNotEqual(len(search), 0)
98103

@@ -150,15 +155,17 @@ def test_get_images_req_url(self):
150155

151156
def test_repr(self):
152157
res = images.ImageResult()
153-
assert repr(res) == 'ImageResult(index=None, page=None, domain=None, link=None)'
158+
assert repr(
159+
res) == 'ImageResult(index=None, page=None, domain=None, link=None)'
154160
res.page = 1
155161
res.index = 11
156162
res.name = 'test'
157163
res.thumb = 'test'
158164
res.format = 'test'
159165
res.domain = 'test'
160166
res.link = 'http://aa.com'
161-
assert repr(res) == 'ImageResult(index=11, page=1, domain=test, link=http://aa.com)'
167+
assert repr(
168+
res) == 'ImageResult(index=11, page=1, domain=test, link=http://aa.com)'
162169

163170
def test_download(self):
164171
pass

google/tests/test_utils.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""Tests helper methods."""
5+
6+
from __future__ import unicode_literals
7+
from __future__ import print_function
8+
from __future__ import with_statement
9+
import unittest
10+
import nose
11+
12+
from google.modules.utils import _get_search_url
13+
14+
15+
class UtilsTestCase(unittest.TestCase):
16+
"""Tests for helper methods."""
17+
18+
def test_get_search_url(self):
19+
url = _get_search_url("apple", 0, 10, "en")
20+
exp_url = "http://www.google.com/search?q=apple&start=0&num=10&nl=en"
21+
self.assertEqual(url, exp_url)
22+
23+
24+
if __name__ == '__main__':
25+
nose.run(defaultTest=__name__)

0 commit comments

Comments
 (0)