@@ -32,76 +32,7 @@ def parse_soup(self, soup):
3232 # find all class_='g' => each result
3333 return soup .find_all ('div' , class_ = 'g' )
3434
35- def parse_result (self , results , ** kwargs ):
36- """
37- Runs every entry on the page through parse_single_result
38-
39- :param results: Result of main search to extract individual results
40- :type results: list[`bs4.element.ResultSet`]
41- :returns: dictionary. Containing lists of titles, links, descriptions, direct results and other possible\
42- returns.
43- :rtype: dict
44- """
45- search_results = dict ()
46- for each in results :
47- try :
48- rdict = self .parse_single_result (each , ** kwargs )
49- # Create a list for all keys in rdict if not exist, else
50- for key in rdict .keys ():
51- if key not in search_results .keys ():
52- search_results [key ] = list ([rdict [key ]])
53- else :
54- search_results [key ].append (rdict [key ])
55- except Exception : #pylint: disable=invalid-name, broad-except
56- pass
57-
58- direct_answer = self .parse_direct_answer (results [0 ])
59- if direct_answer is not None :
60- search_results ['direct_answer' ] = direct_answer
61-
62- return search_results
63-
64- def parse_direct_answer (self , single_result , return_type = ReturnType .FULL ):
65- # returns empty string when there is no direct answer
66- if return_type in (ReturnType .FULL , ReturnType .DESCRIPTION ):
67- direct_answer = ''
68- if not single_result .find ('span' , class_ = 'st' ):
69- # example query: President of US
70- if single_result .find ('div' , class_ = 'Z0LcW' ):
71- direct_answer = single_result .find ('div' , class_ = 'Z0LcW' ).find ('a' ).text
72-
73- # example query: 5+5
74- elif single_result .find ('span' , class_ = 'qv3Wpe' ):
75- direct_answer = single_result .find ('span' , class_ = 'qv3Wpe' ).text
76-
77- # example query: Weather in dallas
78- elif single_result .find ('div' , id = 'wob_wc' ):
79- weather_status = single_result .find ('span' , id = 'wob_dc' ).text
80- temperature = single_result .find ('span' , id = 'wob_tm' ).text
81- unit = single_result .find ('div' , class_ = 'wob-unit' ).find ('span' , class_ = 'wob_t' ).text
82- direct_answer = weather_status + ', ' + temperature + unit
83-
84- # example query: 100 euros in pounds
85- elif single_result .find ('span' , class_ = 'DFlfde SwHCTb' ):
86- direct_answer = single_result .find ('span' , class_ = 'DFlfde SwHCTb' ).text + ' ' + single_result .find ('span' , class_ = 'MWvIVe' ).text
87-
88- # example query: US time
89- elif single_result .find ('div' , class_ = "gsrt vk_bk dDoNo" ):
90- direct_answer = single_result .find ('div' , class_ = 'gsrt vk_bk dDoNo' ).text
91-
92- # Christmas
93- elif single_result .find ('div' , class_ = "zCubwf" ):
94- direct_answer = single_result .find ('div' , class_ = "zCubwf" ).text
95-
96-
97- elif not single_result .find ('span' , class_ = 'st' ).text :
98- # example queris: How long shoud a car service take?, fastest animal
99- if single_result .find ('div' , class_ = 'Z0LcW' ):
100- direct_answer = single_result .find ('div' , class_ = 'Z0LcW' ).text
101-
102- return direct_answer
103-
104- def parse_single_result (self , single_result , return_type = ReturnType .FULL ):
35+ def parse_single_result (self , single_result , return_type = ReturnType .FULL , ** kwargs ):
10536 """
10637 Parses the source code to return
10738
0 commit comments