(req)
return res
Search for a request in Yandex and return results
Search for a request in Yandex and return results
Search for a request in Yandex and return results
def search(req):
        '''
        Search for a request in Yandex and return results
        '''
        r = requests.get('https://yandex.ru/search/?text=%s' %
                urllib.parse.quote(req, safe='')
        )
        soup = BeautifulSoup(r.text, 'lxml')
        res = []
        for l in soup.find_all('li', class_='serp-item'):
            url = l.a['href']
            encoded_url = urllib.parse.quote(url, safe='')
            result = {
                'url': l.a['href'],
                'name': l.a.text,
            }
            text = l.find('div', class_="text-container")
            if(text):
                result['text'] = text.text
            popup = l.find_all('div', class_='popup2')
            if len(popup):
                for link in popup[0].find_all('a'):
                    if 'translate.yandex.ru' in link['href']:
                        if link['href'].startswith('http'):
                            result['translate'] = link['href']
                        else:
                            result['translate'] = 'http:' + link['href']
                    if 'hghltd.yandex.net' in link['href']:
                        result['cache'] = link['href']
            res.append(result)

        return res
def search ( req ) : r = requests . get ( 'https://yandex.ru/search/?text=%s' % urllib . parse . quote ( req , safe = '' ) ) soup = BeautifulSoup ( r . text , 'lxml' ) res = [ ] for l in soup . find_all ( 'li' , class_ = 'serp-item' ) : url = l . a [ 'href' ] encoded_url = urllib . parse . quote ( url , safe = '' ) result = { 'url' : l . a [ 'href' ] , 'name' : l . a . text , } text = l . find ( 'div' , class_ = "text-container" ) if ( text ) : result [ 'text' ] = text . text popup = l . find_all ( 'div' , class_ = 'popup2' ) if len ( popup ) : for link in popup [ 0 ] . find_all ( 'a' ) : if 'translate.yandex.ru' in link [ 'href' ] : if link [ 'href' ] . startswith ( 'http' ) : result [ 'translate' ] = link [ 'href' ] else : result [ 'translate' ] = 'http:' + link [ 'href' ] if 'hghltd.yandex.net' in link [ 'href' ] : result [ 'cache' ] = link [ 'href' ] res . append ( result ) return res