Skip to content

Commit d9962f4

Browse files
committed
fix compatibility for python 3 (< 3.4) #3
1 parent 97996f6 commit d9962f4

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
language: python
22
python:
33
- "2.7"
4+
- "3.2"
45
- "3.6"
6+
# 3.2 added because https://github.com/aviaryan/python-gsearch/issues/3
57
# command to run tests
68
script: python -m unittest tests.tests

gsearch/googlesearch.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
# Python 3
1212
from urllib import request
1313
from html.parser import HTMLParser # keep it to avoid warning
14-
from html import unescape
1514
from urllib.parse import quote, unquote
1615
# local
1716
try:
1817
from gsearch.data import user_agents # works in tests
1918
except ImportError:
2019
from data import user_agents # works in a normal run
20+
try:
21+
from html import unescape # Python 3.4+
22+
except ImportError:
23+
pass
2124
except ImportError:
2225
# Python 2
2326
import urllib2 as request
@@ -86,7 +89,12 @@ def convert_unicode(text):
8689
h = HTMLParser()
8790
s = h.unescape(text)
8891
else:
89-
s = unescape(text)
92+
try:
93+
s = unescape(text)
94+
except Exception:
95+
# Python 3.3 and below
96+
# https://stackoverflow.com/a/2360639/2295672
97+
s = HTMLParser().unescape(text)
9098
return s
9199

92100

tests/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_results_count(self):
1717
self.assertTrue(len(res) > 10, 'Less than 11 results returned')
1818

1919
def test_results_zero(self):
20-
res = search('dsjaksfajsdhkhawkehkajdwek')
20+
res = search('dsjaksfajsdhkhawkehkajdwek' + (str(randint(10,100)) * 5))
2121
self.assertTrue(len(res) == 0, 'There was a result. What has this world come to?')
2222

2323
def test_unicode(self):

0 commit comments

Comments
 (0)