我有一个HTML临时文件,如下所示

<tr>
  <td width="45">
    <p style="text-align: center;"><strong>STT</strong></p>
  </td>
  <td width="204">
    <p style="text-align: center;"><strong>Tên bệnh viện</strong></p>
  </td>
  <td width="364">
    <p style="text-align: center;"><strong>Địa chỉ</strong></p>
  </td>
</tr>,

<tr>
  <td width="45"><strong> </strong>
    <p><strong>1</strong></p>
  </td>
  <td width="204"><strong> </strong>
    <h3><span id="list hospital"><strong> ABC HOSPITAL</strong></span></h3>
  </td>
  <td width="364">
    <img alt="abc hospital" class="aligncenter size-full wp-image-5549" height="470" sizes="(max-width: 705px) 100vw, 705px" src="https://suckhoe2t.net/wp-content/uploads/2017/11/benh-vien-an-binh-suckhoe2t.jpg" srcset="https://suckhoe2t.net/wp-content/uploads/2017/11/benh-vien-an-binh-suckhoe2t.jpg 705w, https://suckhoe2t.net/wp-content/uploads/2017/11/benh-vien-an-binh-suckhoe2t-696x464.jpg 696w, https://suckhoe2t.net/wp-content/uploads/2017/11/benh-vien-an-binh-suckhoe2t-630x420.jpg 630w" width="705"/>
    <p><iframe allowfullscreen="allowfullscreen" frameborder="0" height="450" src="https://www.google.com/maps/embed?pb=!1m18!221m12!1m3!1d3919.743463626014!2d106.66938211450157!3d10.75424379233658!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x31752efc4039dee3%3A0x9157c2008d49be79!2sAn+Binh+Hospital!5e0!3m2!1sen!2s!4v1557202875759!5m2!1sen!2s" style="border: 0;" width="600"></iframe></p>
    <ul>
      <li>address: 1345 Golden View , LA</li>
      <li>phonenumber: 3923 4260</li>
      <li>Email: abc@hospital.com</li>
    </ul>
    <ul>
      <li>Website: <a href="xxxxxx" rel="noopener" target="_blank">xxxxxxxxx</a></li>

我希望有这样的输出:

ABC HOSPITAL
address: 1345 Golden View , LA
phonenumber: 3923 4260
Email: abc@hospital.com

因为它有li多个标签,我不知道如何获得我想要的所有字段.你能帮忙吗?

我的代码如下:

res = '''html code above'''

soup = BeautifulSoup(res, 'html.parser')
data = soup.find_all('tr')

for temp in data:
    each = temp.find('h3')
    print(each)

我得到的输出:

None
<h3><span id="list hospital"><strong> ABC HOSPITAL</strong></span></h3>

推荐答案

这应该行得通.

soup = BeautifulSoup(res, 'html.parser')
data = soup.find_all('tr')

accepted_li = ('address', 'phonenumber', 'email') # tuple of "li" informations you want to get

for tr in data:
    hospital_span = tr.find('span', {'id': 'list hospital'}) # get span of the hospital name
    if hospital_span is not None:
        print(hospital_span.find('strong').text.strip())

    for li in tr.find_all('li'): # iterate over every li
        if li.text.lower().startswith(accepted_li): # check if li element starts with any value in tuple
            print(li.text)

Python相关问答推荐

当值是一个integer时,在Python中使用JMESPath来验证字典中的值(例如:1)

如何使用上下文管理器创建类的实例?

在matplotlib动画gif中更改配色方案

Python Hashicorp Vault库hvac创建新的秘密版本,但从先前版本中删除了密钥

使用Keras的线性回归参数估计

Python中的嵌套Ruby哈希

如何标记Spacy中不包含特定符号的单词?

将输入管道传输到正在运行的Python脚本中

从numpy数组和参数创建收件箱

如何从数据库上传数据到html?

joblib:无法从父目录的另一个子文件夹加载转储模型

dask无groupby(ddf. agg([min,max])?''''

为什么if2/if3会提供两种不同的输出?

以逻辑方式获取自己的pyproject.toml依赖项

搜索按钮不工作,Python tkinter

如何在Python请求中组合多个适配器?

如何使用正则表达式修改toml文件中指定字段中的参数值

如何在Python Pandas中填充外部连接后的列中填充DDL值

获取git修订版中每个文件的最后修改时间的最有效方法是什么?

如何根据一定条件生成段id