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

前端 未结 3 911
失恋的感觉
失恋的感觉 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:17

    I took a different route. I saved the HTML from the request and opened that page, then I inspected the elements. It turns out that the page is different if I open it natively in the Chrome browser compared to what my python request is served. I identified the div with the class that appears to denote a result and supplemented that for the .r - in my case it was .kCrYT

    #! python3
    
    # lucky.py - Opens several Google Search results.
    
    import requests, sys, webbrowser, bs4
    
    print('Googling...') # display text while the google page is downloading
    
    url= 'http://www.google.com.au/search?q=' + ' '.join(sys.argv[1:])
    url = url.replace(' ','+')
    
    
    res = requests.get(url)
    res.raise_for_status()
    
    
    # Retrieve top search result links.
    soup=bs4.BeautifulSoup(res.text, 'html.parser')
    
    
    # get all of the 'a' tags afer an element with the class 'kCrYT' (which are the results)
    linkElems = soup.select('.kCrYT > a') 
    
    # Open a browser tab for each result.
    numOpen = min(5, len(linkElems))
    for i in range(numOpen):
        webbrowser.open_new_tab('http://google.com.au' + linkElems[i].get('href'))
    

提交回复
热议问题