【问题标题】:What is the error in these function and how can i overcome it?这些功能有什么错误,我该如何克服?
【发布时间】:2022-11-19 22:41:47
【问题描述】:

我问了一个问题并得到了成功的答案(link。不幸的是,我在 google colab 中使用建议的代码时遇到了问题。你能帮我吗(i)让建议的代码在 google colab 中工作;或者(ii)建议我在链接中解释的问题的新代码,好吗?

我正在使用代码:


import requests
import pandas as pd
from bs4 import BeautifulSoup

html = requests.get("https://www.tce.sp.gov.br/jurisprudencia/exibir?proc=18955/989/20&offset=0")

soup = BeautifulSoup(html.content)

data = []

for e in soup.select('table:last-of-type tr:has(td)'):
    it = iter(soup.table.stripped_strings)
    d = dict(zip(it,it))
    d.update({
        'link': e.a.get('href'),
        'date': e.select('td')[-2].text,
        'type': e.select('td')[-1].text
    })
    data.append(d)

但它返回此错误:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-14-c9c2af04191b> in <module>
      9 data = []
     10 
---> 11 for e in soup.select('table:last-of-type tr:has(td)'):
     12     it = iter(soup.table.stripped_strings)
     13     d = dict(zip(it,it))

/usr/local/lib/python3.7/dist-packages/bs4/element.py in select(self, selector, _candidate_generator, limit)
   1526                 else:
   1527                     raise NotImplementedError(
-> 1528                         'Only the following pseudo-classes are implemented: nth-of-type.')
   1529 
   1530             elif token == '*':

NotImplementedError: Only the following pseudo-classes are implemented: nth-of-type.

【问题讨论】:

  • 您使用的是什么版本的beautifulsoup
  • 我不知道如何在 colab 中检查它。我知道我的 python 版本是 Python 3.7.15
  • 尝试更新您的beautifulsoup版本,您似乎使用的是旧版本。
  • change python version 也可能确保你安装了 html5lib 解析器 [对 bs 和 html 使用 !pip show...]

标签: python web-scraping select beautifulsoup google-colaboratory


【解决方案1】:

您的代码完美运行,完全没有错误。只需升级“BeautifulSoup”。

pip install --upgrade beautifulsoup4

其余代码将相同。

注意:升级 BeautifulSoup 库后,请重新启动 colab 环境的运行时,以便升级后的库生效。

重启运行时的步骤:

Click on Runtime menu.
Select Restart runtime.
Select Run all.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2020-04-17
    • 2022-07-29
    • 1970-01-01
    相关资源
    最近更新 更多