谷歌词典搜索结果

2024-05-29 09:41:00 发布

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

我正试图从谷歌的字典中找出结果,但我认为我犯了一些错误。以下是我试图抓取的内容的截图:

1

以下是我正在运行的代码:

elif 'meaning' in query:
          query = query.replace("what", "").replace("meaning", "").replace("means", "").replace("is the", "").replace("of","")
          headers = {
            'User-agent':
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
          }
          r = requests.get(f"https://www.google.com/search?q={query}", headers=headers)
          soup = BeautifulSoup(r.text, 'lxml')

          result = soup.find('div', class_= 'LTKOO sY7ric')
          print(result.span.text)
          speak(result.span.text)

我得到了这个错误:

Traceback (most recent call last):
  File "c:\MY Programmes\DUSTIN- THE VOICE ASSISTANT\dustin.py", line 106, in <module>
    print(result.span.text)
AttributeError: 'NoneType' object has no attribute 'span'

Tags: 代码textin内容字典错误resultquery
1条回答
网友
1楼 · 发布于 2024-05-29 09:41:00

最可能的情况是,当您尝试soup.find(query)时,页面尚未加载,因此您可以执行time.sleep(x)或使用wget下载页面,并在下载后使用soup查询该页面

资料来源:BeautifulSoup find class return none

相关问题 更多 >

    热门问题