@@ -32,7 +32,6 @@ def __repr__(self):
32
32
33
33
def _limit_str_size (self , str_element , size_limit ):
34
34
"""Limit the characters of the string, adding .. at the end."""
35
-
36
35
if not str_element :
37
36
return None
38
37
@@ -44,7 +43,7 @@ def _limit_str_size(self, str_element, size_limit):
44
43
45
44
46
45
# PUBLIC
47
- def search (query , pages = 1 ):
46
+ def search (query , pages = 1 , lang = 'en' ):
48
47
"""Returns a list of GoogleResult.
49
48
50
49
Args:
@@ -56,16 +55,17 @@ def search(query, pages=1):
56
55
57
56
results = []
58
57
for i in range (pages ):
59
- url = _get_search_url (query , i )
58
+ url = _get_search_url (query , i , lang = lang )
60
59
html = get_html (url )
61
60
62
61
if html :
63
62
soup = BeautifulSoup (html , "html.parser" )
64
63
lis = soup .findAll ("li" , attrs = {"class" : "g" })
65
-
64
+
66
65
j = 0
67
66
for li in lis :
68
67
res = GoogleResult ()
68
+
69
69
res .page = i
70
70
res .index = j
71
71
@@ -86,7 +86,10 @@ def search(query, pages=1):
86
86
def _get_name (li ):
87
87
"""Return the name of a google search."""
88
88
a = li .find ("a" )
89
- return a .text .strip ()
89
+ #return a.text.encode("utf-8").strip()
90
+ if a is not None :
91
+ return a .text .strip ()
92
+ return None
90
93
91
94
92
95
def _get_link (li ):
@@ -123,9 +126,9 @@ def _get_description(li):
123
126
sdiv = li .find ("div" , attrs = {"class" : "s" })
124
127
if sdiv :
125
128
stspan = sdiv .find ("span" , attrs = {"class" : "st" })
126
-
127
- return stspan .text .encode ("utf-8" ).strip ()
128
-
129
+ if stspan is not None :
130
+ # return stspan.text.encode("utf-8").strip()
131
+ return stspan . text . strip ()
129
132
else :
130
133
return None
131
134
0 commit comments