How to use BeautifulSoup to parse google search results in Python

前端 未结 1 1845
野的像风
野的像风 2021-01-03 09:41

I am trying to parse the first page of google search results. Specifically, the Title and the small Summary that is provided. Here is what I have so far:

fro         


        
1条回答
  •  北海茫月
    2021-01-03 10:27

    Your url doesn't work for me. But with https://google.com/search?q= I get results.

    import urllib
    from bs4 import BeautifulSoup
    import requests
    import webbrowser
    
    text = 'hello world'
    text = urllib.parse.quote_plus(text)
    
    url = 'https://google.com/search?q=' + text
    
    response = requests.get(url)
    
    #with open('output.html', 'wb') as f:
    #    f.write(response.content)
    #webbrowser.open('output.html')
    
    soup = BeautifulSoup(response.text, 'lxml')
    for g in soup.find_all(class_='g'):
        print(g.text)
        print('-----')
    

    Read Beautiful Soup Documentation

    0 讨论(0)
提交回复
热议问题