且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用BeautifulSoup在Python中解析Google搜索结果

更新时间:2023-02-23 16:09:41

您的网址对我不起作用.但是用https://google.com/search?q=我得到结果.

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('-----')

阅读 Beautiful Soup文档