我正在使用Python 3.7和BeautifulSoup4。我想找到在“ 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()?
什么给?
答案 0 :(得分:0)
使用:
soup.find_all(".class1.class2")
答案 1 :(得分:0)
将find()用于td,将find_all()用于div元素
elts = soup.find("td", class_="info").find_all("div", class_="title")
for div in elts:
print(div['title'])
如果您有多个td标签,而在td下则有多个div标签,请尝试以下操作。
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'])