Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BeautifulSoup-帮我挑选div和类_Python_Html_Json_Beautifulsoup - Fatal编程技术网

Python BeautifulSoup-帮我挑选div和类

Python BeautifulSoup-帮我挑选div和类,python,html,json,beautifulsoup,Python,Html,Json,Beautifulsoup,这是我的HMTL代码: <div class="BlockA"> <h4>BlockA</h4> <div class="name">John Smith</div> <div class="number">2</div> <div class="name">Paul Peterson</div> <div class="number">

这是我的HMTL代码:

<div class="BlockA">
    <h4>BlockA</h4>
    <div class="name">John Smith</div>
    <div class="number">2</div>
    <div class="name">Paul Peterson</div>
    <div class="number">14</div>
</div>

<div class="BlockB">
    <h4>BlockB</h4>
    <div class="name">Steve Jones</div>
    <div class="number">5</div>
</div>
但这只是给了我一个空白屏幕。我是否可以从
blockA
中执行
findAll
,显示数据,然后从
BlockB
启动另一个循环并执行相同的操作

谢谢

编辑:对于那些提问的人,我想简单地循环使用JSON中的值和输出,如下所示:

BlockA
    John Smith
    2
    Paul Peterson
    14

BlockB
    Steve Whoever
    123
    Mr Whathisface
    23

是否要查找包含“name”或“number”类属性的div

>>重新导入
>>>findAll(“div”,“class”:re.compile(“name | number”)})
[约翰·史密斯,2岁,保罗·彼得森,14岁,史蒂夫·琼斯,5岁]

您需要使用一个可能的
值列表

soup.findAll('div', {'class': ['name', 'number']})
查看您的编辑后:

def grab_content(heading):
    siblings = [s.contents[0] for s in heading.findNextSiblings()]
    return {heading.contents[0]: siblings}

headings = soup.findAll('h4')
[grab_content(h) for h in headings]
原始HTML代码段的输出为:

[{u'BlockA': [u'John Smith', u'2', u'Paul Peterson', u'14']},
 {u'BlockB': [u'Steve Jones', u'5']}]

你还没有告诉我们你想做什么!;-)你想用attrs={“name”:“number”}做什么????也许你想仔细阅读BeautifulSoup(!)并使用{'class':'number'}或其他什么……谢谢你的回复。如果我使用
return-HttpResponse(h)
return-HttpResponse(h.content)在最后输出时只返回
BlockB
finadAll(h4)
。有什么我遗漏的吗?
def grab_content(heading):
    siblings = [s.contents[0] for s in heading.findNextSiblings()]
    return {heading.contents[0]: siblings}

headings = soup.findAll('h4')
[grab_content(h) for h in headings]
[{u'BlockA': [u'John Smith', u'2', u'Paul Peterson', u'14']},
 {u'BlockB': [u'Steve Jones', u'5']}]