使用 BeautifulSoup,如何在类中搜索元素类?

戴夫

我正在使用 Python 3.7 和 BeautifulSoup 4。我想找到出现在“td.info”元素中的所有“div.title”元素。CSS 选择器(我相信)看起来像

td.info div.title

所以我想我可以得到这样的元素

elts = soup.findAll("td", {"class": "info"}).find("div", {"class": "title"})
for div in elts:

但相反我收到错误

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/davea/Documents/workspace/myproject_project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/davea/Documents/workspace/myproject_project/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/davea/Documents/workspace/myproject_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/davea/Documents/workspace/myproject_project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/Users/davea/Documents/workspace/myproject_project/myproject/management/commands/runstats.py", line 11, in handle
    ret = MediaService.check_url("https://i.redd.it/wazz3axjtk331.jpg")
  File "/Users/davea/Documents/workspace/myproject_project/myproject/services/media_service.py", line 42, in check_url
    results = json.loads(MediaService.parseResults(code, True))
  File "/Users/davea/Documents/workspace/myproject_project/myproject/services/media_service.py", line 96, in parseResults
    elts = soup.findAll("td", {"class": "info"}).find("div", {"class": "title"})
  File "/Users/davea/Documents/workspace/myproject_project/venv/lib/python3.7/site-packages/bs4/element.py", line 1621, in __getattr__
    "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

是什么赋予了?

昆杜克

对 td 使用 find(),对 div 元素使用 find_all()

elts = soup.find("td", class_="info").find_all("div", class_="title")
for div in elts:
    print(div['title'])

如果您有多个 td 标签,如果您有多个 div 标签,则在 td 下尝试以下操作。


for td in soup.find_all("td", class_="info"):
    for div in td.find_all("div", class_="title"):
        print(div['title'])

使用 css

for td in soup.select("td.info"):
    for div in td.select("div.title"):
        print(div['title'])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在类中使用List <String>元素创建不可变的类

如何在yii2中使用WHERE-LIKE聚类,并使LIKE模式搜索区分大小写?

如何使用BeautifulSoup选择href类标签?

如何使用JavaScript在类元素中搜索文本

如何在类外使用方法对象(在类中)?

如何在不使用类名的情况下从类中访问类本身

如何使用Beautifulsoup4刮除属性中未指定类或ID的HTML元素

如何在BeautifulSoup中使用find_all来搜索多个标签或类?

如何在JS中使用类选择所有元素

如何使用JNI访问C ++中Java类的元素

如何使用Python和BeautifulSoup解析类

如何使用Iterator类从列表中删除重复的元素?

如何使用列表中的元素为类创建对象

如何使用Python和BeautifulSoup中的类过滤标签?

如何在Jquery和html5中使用相同的类管理不同的元素

如何在BeautifulSoup中的类中使用re.compile

使用beautifulsoup如何从具有多个类的元素中删除单个类

如何使用beautifulsoup从html标记的特定类中获取数据?

如何使用jQuery从html元素中添加或删除类

如何在类内部通过类名调用元素并使用它

如何使用beautifulsoup获取没有任何类或id的特定ul元素li的文本和href

我如何在 jquery 中使用类存储元素值

如何在 Fragment 类中使用布局元素

如何使用 JS 从多个 ID 元素中删除 CSS 类?

如何从使用 addEventListener 添加的事件的元素中删除类?

如何使用beautifulsoup提取锚类?

使用 BeautifulSoup 抓取 CSS 类中的特定元素

如何使用 selenium 在 div 类中获取 a-tag 的元素?

如何使用 BeautifulSoup 在网页上查找特定的类元素