【问题标题】:Google Dictionary result scraping谷歌字典结果抓取
【发布时间】:2021-09-05 04:31:57
【问题描述】:

我正在尝试抓取 google 的字典结果,但我认为我做错了。这是我要抓取的内容的屏幕截图:

这是我正在运行的代码:

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'

【问题讨论】:

    标签: python python-3.x web-scraping


    【解决方案1】:

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

    来源:BeautifulSoup find class return none

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 2018-12-19
      • 2020-10-09
      • 1970-01-01
      相关资源
      最近更新 更多