soup.select('.r a') in f'https://google.com/search?q={query}' brings back empty list in Python BeautifulSoup. **NOT A DUPLICATE**

前端 未结 3 910
失恋的感觉
失恋的感觉 2020-11-30 14:42

The \"I\'m Feeling Lucky!\" project in the \"Automate the boring stuff with Python\" ebook no longer works with the code he provided.

Specifically, the linkElems = s

3条回答
  •  抹茶落季
    2020-11-30 15:31

    I too had had the same problem while reading that book and found a solution for that problem.

    replacing

    soup.select('.r a')
    

    with

    soup.select('div#main > div > div > div > a')
    

    will solve that issue

    following is the code that will work

    import webbrowser, requests, bs4 , sys
    
    print('Googling...')
    res = requests.get('https://google.com/search?q=' + ' '.join(sys.argv[1:]))
    res.raise_for_status()
    
    soup = bs4.BeautifulSoup(res.text)
    
    linkElems = soup.select('div#main > div > div > div > a')  
    numOpen = min(5, len(linkElems))
    for i in range(numOpen):
        webbrowser.open('http://google.com' + linkElems[i].get("href"))
    

    the above code takes input from commandline arguments

提交回复
热议问题