我正试图刮谷歌知识面板检索药物的名称,如果他们没有出现在谷歌搜索。例如,如果我在Google中查找“Buscopan”,出现的网页如下所示:
现在,我试图对所示代码执行的操作是在知识面板中使用术语“东莨菪碱-N-butilbromuro”,但在检查元素后,实际上无法在html代码中检索它。准确地说。我正在实现的代码以及错误消息如下:
import requests
from bs4 import BeautifulSoup
url=”https://www.google.com/search?client=safari&;rls=en&;q=“+”巴士公司“+”和ie=UTF-8和oe=UTF-8”
# Sending HTTP request
req = requests.get(url)
# Pulling HTTP data from internet
sor = BeautifulSoup(req.text, "html.parser")
temp = sor.find("h2", class_= "qrShPb kno-ecr-pt PZPZlf mfMhoc hNKfZe").text
print(temp)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-39-ef5599a1a1fc> in <module>
13 # Finding temperature in Celsius
14 #temp = sor.find("h2", class_='qrShPb').text
---> 15 temp = sor.find("h2", class_= "qrShPb kno-ecr-pt PZPZlf mfMhoc hNKfZe").text
16
17
AttributeError: 'NoneType' object has no attribute 'text'
我不知道我做错了什么。我认为我需要看的html代码如下:
<h2 class="qrShPb kno-ecr-pt PZPZlf mfMhoc hNKfZe" data-local-attribute="d3bn" data-attrid="title" data-ved="2ahUKEwjujfLcgO7rAhWKjosKHSiBAFEQ3B0oATASegQIEBAL"></h2>
当然,其余的html代码都在报告的图片中,但是如果您需要更大的版本,请不要声明
有什么建议吗
谢谢,
费德里科
或者,对于Andrej Kesely解决方案,您可以使用来自SerpApi的第三方Google Knowledge Graph API。这是一个免费的付费API。查看要测试的Playground
集成和test example的代码:
输出:
JSON输出的一部分:
要从Google搜索中获得正确的结果页面,请指定
User-Agent
HTTP头。例如:印刷品:
相关问题 更多 >
编程相关推荐