Python Beautifulsoup4-将两列表解析到字典中

Python Beautifulsoup4-将两列表解析到字典中,python,beautifulsoup,Python,Beautifulsoup,我正在使用Beautifulsoup4 我有一个大页面要解析,但我需要找到该部分 soup.findAll('h2',text='Case details') 我想创建以下对象 详情={'Court:':'nysb'} 如何找到该部分,然后遍历下一个表,该表是一个两列的表,并将第一列作为散列中的键,第二列作为值 <body> <h2> Case details </h2> <table> <tr> <

我正在使用Beautifulsoup4

我有一个大页面要解析,但我需要找到该部分

soup.findAll('h2',text='Case details')

我想创建以下对象

详情={'Court:':'nysb'}

如何找到该部分,然后遍历下一个表,该表是一个两列的表,并将第一列作为散列中的键,第二列作为值

<body>
  <h2>
   Case details
  </h2>
  <table>
   <tr>
    <td>
     <b>
      Court:
     </b>
    </td>
    <td>
     nysb
    </td>
   </tr>
   </table>
</body>

table = h2_details.find_next_sibling('table')
AttributeError: 'ResultSet' object has no attribute 'find_next_sibling'

个案详情
法庭:
纽约证券交易所
table=h2\u详细信息。查找下一个兄弟姐妹('table')
AttributeError:'ResultSet'对象没有“查找下一个兄弟姐妹”属性
用于查找
H2
标记后面的表格,然后从那里获取:

h2_details = soup.find('h2', text='Case details')

table = h2_details.find_next_sibling('table')

details = {}
for row in table.find_all('tr'):
    cells = row.find_all('td', limit=2)
    details[cells[0].string] = cells[1].string

我在这里使用了
.string
,假设每个表单元格只包含文本(没有标记)。如果有标记,可能您希望使用
'.join(单元格[0]。剥离的\u字符串)
'.join(单元格[1]。剥离的\u字符串)

Hi..我添加了在底部运行代码时遇到的错误,如果有问题。下面是我如何从bs4导入->的步骤BeautifulSoup@Tampa:很抱歉,没有注意到您正在使用
.findAll()
;使用
.find()
或循环查看
h2\u详细信息
并对每个匹配项运行表搜索。