有没有一种方法可以创建一个python代码,将谷歌搜索链接下的描述作为文本阅读



我读了很多关于应该安装哪些库来打印URL链接或打开web链接的文章,但我对应该做什么或安装什么感到困惑,因为它们不满足我的需求。我需要一种方法,如果我输入一个要搜索的句子,它会在输出中将URL下的描述打印为字符串:如图所示:我需要的示例

我使用IDLE(python 3.9 64位(。

使用Selenium控制真实web浏览器的示例代码。

首先打开浏览器并加载页面google.com
接下来,它将关闭弹出对话框,要求获取cookie
接下来,它将填充搜索字段并单击ENTER
最后用class="g"获取所有<div>,并搜索所有可能有文本的<span>

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
query = 'what is the sound of elephant'
url = 'https://google.com/'
driver = webdriver.Firefox()
driver.get(url)
time.sleep(2)
# --- close popup dialog ---
items = driver.find_elements_by_xpath('//button[div[@role]]')
items[1].click()
time.sleep(2)
# --- fill input ---
items = driver.find_elements_by_xpath('//input[@maxlength]')
items[0].send_keys(query + Keys.ENTER)
time.sleep(2)
# --- get results ---
items = driver.find_elements_by_xpath('//div[@class="g"]')  # absolute xpath 
print('len(items):', len(items))
for item_number, item in enumerate(items):
print('n--- item:', item_number, '---n')
spans = item.find_elements_by_xpath('./div/div/div/div/span')  # relative xpath
for number, s in enumerate(spans):
print('- span:', number, '-')
print(s.text.strip())

结果:

因为谷歌可以在网络浏览器(波兰语(中检测到我的语言设置,所以一些结果是波兰语(即日期(

len(items): 10
--- item: 0 ---

--- item: 1 ---

--- item: 2 ---

--- item: 3 ---

--- item: 4 ---
- span: 0 -
Trumpeting is the most famous and recognisable sound from an elephant's vocabulary, its high-pitched tone heard for miles around. However, ...
- span: 1 -
Przesłany przez: Kim Schnarr
--- item: 5 ---
- span: 0 -
What sounds do elephants make? · Trumpeting · Rumbling · Roaring · Chirps and Squeaks · Ear Flapping · Bubbling.
--- item: 6 ---
- span: 0 -
6 paź 2017 —
- span: 1 -
Elephants like this African forest elephant make trumpeting sounds when they are excited, fearful, or happy. ... Please be respectful of copyright.
--- item: 7 ---
- span: 0 -
The range of sounds elephants produce ... African elephants produce a broad range of sounds from very low frequency Rumbles to higher frequency Snorts, Barks, ...
--- item: 8 ---
- span: 0 -
12 sie 2015 —
- span: 1 -
Elephants make a sound, known as a trumpet, to signal excitement, aggression and distress. This trumpeting can be heard from up to six miles ...
--- item: 9 ---
- span: 0 -
30 gru 2016 —
- span: 1 -
Scott Pettit: Elephants don't say anything. They trumpet. So you have to verbally interpret a trumpeting sound. “Pawoooo” isn't too bad, actually.

相关内容

  • 没有找到相关文章

最新更新