谷歌靓汤刮痧

2024-09-30 00:30:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图刮谷歌知识面板检索药物的名称,如果他们没有出现在谷歌搜索。例如,如果我在Google中查找“Buscopan”,出现的网页如下所示: enter image description here

现在,我试图对所示代码执行的操作是在知识面板中使用术语“东莨菪碱-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代码都在报告的图片中,但是如果您需要更大的版本,请不要声明

有什么建议吗

谢谢,

费德里科


Tags: 代码textptdatahtmlh2findtemp
2条回答

或者,对于Andrej Kesely解决方案,您可以使用来自SerpApi的第三方Google Knowledge Graph API。这是一个免费的付费API。查看要测试的Playground

集成和test example的代码:

from serpapi import GoogleSearch
import os

params = {
  "q": "Buscopan",
  "google_domain": "google.com",
  "hl": "en",
  "api_key": os.getenv("API_KEY"),
}

search = GoogleSearch(params)
results = search.get_dict()

title = results['knowledge_graph']['title']
print(title)

输出:

Butylscopolamine

JSON输出的一部分:

"knowledge_graph": {
  "title": "Butylscopolamine",
  "type": "Medication",
  "description": "Hyoscine butylbromide, also known as scopolamine butylbromide and sold under the brandname Buscopan among others, is an anticholinergic medication used to treat crampy abdominal pain, esophageal spasms, renal colic, and bladder spasms. It is also used to improve respiratory secretions at the end of life.",
  "source": {
    "name": "Wikipedia",
    "link": "https://en.wikipedia.org/wiki/Hyoscine_butylbromide"
  },
  "formula": "C₂₁H₃₀BrNO₄",
  "molar_mass": "440.371 g/mol",
  "chem_spider_id": "16736107",
  "trade_name": "Buscopan, others",
  "pub_chem_cid": "6852391",
  "ch_ebi_id": "32123",
  "people_also_search_for": "Scopolamine, Metamizole, MORE"
}

Disclaimer, I work for SerpApi.

要从Google搜索中获得正确的结果页面,请指定User-AgentHTTP头。例如:

import requests 
from bs4 import BeautifulSoup


params = {
    'q': 'buscopan',    # <  change to your keyword
    'hl': 'it'          # <  change to `en` for english results
}

headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'}
url = 'https://www.google.com/search'
soup = BeautifulSoup(requests.get(url, params=params, headers=headers).content, 'html.parser')

print(soup.select_one('h2[data-attrid="title"]').text)

印刷品:

Scopolamina-N-butilbromuro

相关问题 更多 >

    热门问题