适用于 Google 搜索结果的 sn-ps(描述)的 CSS 选择器是 .aCOpRe span:not(.f)。
这里是a full example in online IDE。
from bs4 import BeautifulSoup
import requests
import re
param = {"q": "coffee"}
headers = {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15"
}
r = requests.get("https://google.com/search", params=param, headers=headers)
soup = BeautifulSoup(r.content, "lxml")
soup.prettify()
title = soup.select(".DKV0Md span")
for t in title:
print(f"Title: {t.get_text()}\n")
snippets = soup.select(".aCOpRe span:not(.f)")
for d in snippets:
print(f"Snippet: {d.get_text()}\n")
link = soup.findAll("a")
for link in soup.find_all("a", href=re.compile("(?<=/url\?q=)(htt.*://.*)")):
print(re.split(":(?=http)", link["href"].replace("/url?q=", "")))
输出
Title: Coffee - Wikipedia
Title: Coffee: Benefits, nutrition, and risks - Medical News Today
...
Snippet: Coffee is a brewed drink prepared from roasted coffee beans, the seeds of berries from certain Coffea species. When coffee berries turn from green to bright red in color – indicating ripeness – they are picked, processed, and dried.
Snippet: When people think of coffee, they usually think of its ability to provide an energy boost. ... This article looks at the health benefits of drinking coffee, the evidence ...
...
或者,您可以通过 SerpApi 从 Google 搜索中提取数据。
curl 示例
curl -s 'https://serpapi.com/search?q=coffee&location=Sweden&google_domain=google.se&gl=se&hl=sv&num=100'
Python 示例
from serpapi import GoogleSearch
import os
params = {
"engine": "google",
"q": "coffee",
"location": "Sweden",
"google_domain": "google.se",
"gl": "se",
"hl": "sv",
"num": 100,
"api_key": os.getenv("API_KEY")
}
client = GoogleSearch(params)
data = client.get_dict()
print("Organic results")
for result in data['organic_results']:
print(f"""
Title: {result['title']}
Link: {result['link']}
Position: {result['position']}
Snippet: {result['snippet']}
""")
输出
Organic results
Title: Coffee - Wikipedia
Link: https://en.wikipedia.org/wiki/Coffee
Position: 1
Snippet: Coffee is a brewed drink prepared from roasted coffee beans, the seeds of berries from certain Coffea species. When coffee berries turn from green to bright red ...
Title: Drop Coffee
Link: https://www.dropcoffee.com/
Position: 2
Snippet: Drop Coffee is an award winning roastery in Stockholm, representing Sweden four times in the World Coffee Roasting Championship, placing second, third and ...
...
免责声明:我在 SerpApi 工作。