如果您只需要提取一个元素,请使用select_one() bs4 方法。它比find() 更具可读性和速度。 CSS 选择器 reference.
如果您需要非常快速地提取数据,请尝试使用selectolax,它是lexbor HTML Renderer 库的包装,用纯C 编写,没有依赖关系,以及it's fast。
代码和example in the online IDE:
import requests, lxml
from bs4 import BeautifulSoup
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
}
params = {
"q": "fus ro dah definition", # query
"gl": "us", # country
"hl": "en" # language
}
response = requests.get('https://www.google.com/search',
headers=headers,
params=params)
soup = BeautifulSoup(response.text, 'lxml')
# .previous_sibling will go to, well, previous sibling removing unwanted part: "(0.38 seconds)"
number_of_results = soup.select_one('#result-stats nobr').previous_sibling
print(number_of_results)
# About 107,000 results
或者,您可以使用来自 SerpApi 的 Google Organic Results API 来实现相同的目的。这是一个带有免费计划的付费 API。
您的情况的不同之处在于,您唯一需要做的就是从所需的结构化 JSON 中获取数据,而不是弄清楚如何提取某些元素或如何绕过 Google 的阻止。
import os
from serpapi import GoogleSearch
params = {
"engine": "google",
"q": "fus ro dah defenition",
"api_key": os.getenv("API_KEY"),
}
search = GoogleSearch(params)
results = search.get_dict()
result = results["search_information"]['total_results']
print(result)
# 107000
P.S - 我写了一篇关于如何抓取 Google Organic Results 的博文。
免责声明,我为 SerpApi 工作。