【问题标题】:Beautiful Soup find all occurrences of tag between other tags for html pageBeautiful Soup 在 html 页面的其他标签之间找到所有出现的标签
【发布时间】:2016-06-11 00:41:54
【问题描述】:

我有一个包含以下标签的网页:

('span', {'class' : 'block-title'})
('div', {'class' : 'object-title'})
('span', {'class' : 'data-value'})

我可以使用 for 循环遍历页面,并使用以下内容提取我需要的数据

for a in soup.find_all('span', {'class' : 'block-title'}):
print a

for b in soup.find_all('div', {'class' : 'object-title'})
print b 

for c in soup.find_all('span', {'class' : 'data-value'})
print c

我遇到的问题是它在三个单独的列表中提供了它们-title 等我认为这可能与查找下一个或查找兄弟但正在寻找一些帮助/建议

非常感谢

【问题讨论】:

    标签: python html python-2.7 web-scraping beautifulsoup


    【解决方案1】:

    您可以使用第一次find_all() 调用的结果,并通过将列表传递给下一个find_all() 调用来查找其中的所有相关标签。像这样的:

    for a in soup.find_all('span', {'class' : 'block-title'}):
        print a
        for b in print a.find_all(['div', 'span'], {'class' : ['object-title', 'data-value']}):
            print b
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-31
      • 2016-07-06
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 2020-08-26
      • 2018-01-17
      相关资源
      最近更新 更多