@@ -25,7 +25,9 @@ def dogpileimage(text):
2525 data = soup .find_all ("script" )[6 ].string
2626 link_re = re .compile ('"url":"(.*?)",' )
2727 linklist = link_re .findall (data )
28- # linklist = soup.find('div', id="webResults").find_all('a', {'class':'resultThumbnailLink'})
28+ if not linklist :
29+ return "No results returned."
30+
2931 image = parse .unquote (parse .unquote (random .choice (linklist )).split ('ru=' )[1 ].split ('&' )[0 ])
3032 return image
3133
@@ -38,8 +40,12 @@ def dogpile(text):
3840 r = requests .get (web_url , params = params , headers = HEADERS )
3941 r .raise_for_status ()
4042 soup = BeautifulSoup (r .content )
43+ results = soup .find ('div' , id = "webResults" )
44+ if not results :
45+ return "No results found."
46+
4147 result_url = parse .unquote (
42- parse .unquote (soup . find ( 'div' , id = "webResults" ) .find_all ('a' , {'class' : 'resultDisplayUrl' })[0 ]['href' ]).split (
48+ parse .unquote (results .find_all ('a' , {'class' : 'resultDisplayUrl' })[0 ]['href' ]).split (
4349 'ru=' )[1 ].split ('&' )[0 ])
44- result_description = soup . find ( 'div' , id = "webResults" ) .find_all ('div' , {'class' : 'resultDescription' })[0 ].text
50+ result_description = results .find_all ('div' , {'class' : 'resultDescription' })[0 ].text
4551 return "{} -- \x02 {}\x02 " .format (result_url , result_description )
0 commit comments