BS4:使用Python在Google SERP中查找父类

时间:2019-11-01 18:29:26

标签: python-3.x for-loop web-scraping beautifulsoup typeerror

我正在寻找Google搜索引擎的头条新闻。问题是,当我创建一个for循环时,出现“ TypeError:find()不带关键字参数”的问题。

很容易,当我找到解决方案时,我只需要从源代码中删除“ .text”(代码如下所示)。但是,当我这样做时,我得到一个不同的错误:“ TypeError:类型为'Response'的对象没有len()。我想知道是否有解决方法吗?我在下面提供的代码带有“ .text”。包括在内。想知道是否有人能够找到解决方案。

-encodeToCommandBuffer:sourceTexture:destinationTexture:

我希望从Google搜索引擎结果页面返回所有十个标题。

1 个答案:

答案 0 :(得分:0)

首先找到所有BNeawe vvjwJb AP7Wnd类,然后遍历所有结果。

import requests
from bs4 import BeautifulSoup

source = requests.get("https://www.google.com/search?q=online+education").text

soup = BeautifulSoup(source, 'lxml')

headlines = soup.find_all("div", class_="BNeawe vvjwJb AP7Wnd")

for headline in headlines:
    print(headline.text, end=', ')

输出:

What is online education | Definition of Online education is ..., Online Education | Encyclopedia.com, 5 Advantages Of Online Learning: Education Without Leaving Home ..., Online Education & Teaching Courses | Harvard University, What is online education? - Lynda.com, What is Online Education? - Online-Education.net, 50 Top Online Learning Sites - Best College Reviews, Benefits of Online Education | Community College of Aurora in ..., 10 Advantages of Taking Online Classes | OEDB.org, Online learning in higher education - Wikipedia,