Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从google搜索页面中刮取片段文本_Python_Html_Selenium_Web Scraping_Google Search - Fatal编程技术网

Python 从google搜索页面中刮取片段文本

Python 从google搜索页面中刮取片段文本,python,html,selenium,web-scraping,google-search,Python,Html,Selenium,Web Scraping,Google Search,当我们在谷歌搜索一个问题时,它通常会在一个片段中生成一个答案,如下所示: 我的目标是在我的python代码中删除这个文本(“1961年8月4日”,截图中用红色标记包围) 在尝试刮取文本之前,我使用以下代码将web响应存储在文本文件中: page=requests.get(“https://www.google.com/search?q=when+巴拉克+奥巴马+出生”) soup=BeautifulSoup(page.content,'html.parser') out\u file=open

当我们在谷歌搜索一个问题时,它通常会在一个片段中生成一个答案,如下所示:

我的目标是在我的python代码中删除这个文本(“1961年8月4日”,截图中用红色标记包围)

在尝试刮取文本之前,我使用以下代码将web响应存储在文本文件中:

page=requests.get(“https://www.google.com/search?q=when+巴拉克+奥巴马+出生”)
soup=BeautifulSoup(page.content,'html.parser')
out\u file=open(“web\u response.txt”,“w”,encoding='utf-8')
out_file.write(soup.prettify())
inspect element部分中,我注意到代码片段位于div class
Z0LcW XcVN5d
内部(在屏幕截图中以绿色标记包围)。但是,我的txt文件中的响应不包含此类文本,更不用说类名了

我还尝试过作者使用id
rhs\u block
刮取项目。但我的回复中没有这样的id

我在我的回复txt文件中搜索了“1961年8月4日”事件,并试图理解它是否可能是这个片段。但这些事情似乎都不是我想要的

我的计划是获取代码段的div id或类名,并按如下方式查找其内容:

#这是一个伪代码
containers=soup.find_all(class或id='something')
对于容器中的标签:
打印(f“标记文本:{tag.text}”)
有没有办法做到这一点


注意:我也可以使用除beautifulsoup和requests之外的库,只要它能产生结果。

Selenium将产生您需要的结果。 这很方便,因为您可以添加任何等待,并查看屏幕上实际发生的情况

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')

driver.get('https://google.com/')
assert "Google" in driver.title
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".gLFyf.gsfi")))
input_field = driver.find_element_by_css_selector(".gLFyf.gsfi")
input_field.send_keys("how many people in the world")
input_field.send_keys(Keys.RETURN)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".Z0LcW.XcVN5d")))
result = driver.find_element_by_css_selector(".Z0LcW.XcVN5d").text
print(result)
driver.close()
driver.quit()
结果可能会让你感到惊讶:)


您需要安装
Selenium
Chromedriver
。您需要将Chromedriver可执行文件放在Windows的路径中,或者显示Linux的路径。我的例子是Linux。

不需要使用
Selenium
,您可以使用
请求
BS4
来实现这一点,因为您需要的所有内容都位于HTML中,并且没有动态JavaScript

中的代码和示例:

从bs4导入美化组
导入请求,lxml
标题={
“用户代理”:
“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582”
}
html=requests.get('https://www.google.com/search?q=Barack 奥巴马出生日期',页眉=页眉)。文本
soup=BeautifulSoup(html,“lxml”)
born=汤。选择一个('.XcVN5d')。文本
年龄=汤。选择一个('.kZ91ed')。文本
印刷品(出生)
印刷品(年龄)
输出:

1961年8月4日
年龄59岁