Python 在div内部刮取,但不知道如何将元素拆分为列表项,以便它们在生成的csv中显示在新行中

Python 在div内部刮取,但不知道如何将元素拆分为列表项,以便它们在生成的csv中显示在新行中,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正试图从下面的块中刮取中的文本 我设法刮去了上面注释的元素中的文本,问题是中的元素我不知道如何将元素作为列表项分开。。。请导游。下面是我试图解析的html,下面是我使用的代码。。如果可能的话,还可以发布一个解决方案,将它们全部作为单个数组拉入 <div class="column-block" id="hematology"> <h3 class="panel-title names strong"> <

我正试图从下面的块中刮取
中的文本

我设法刮去了上面注释的元素中的文本,问题是
中的元素我不知道如何将元素作为列表项分开。。。请导游。下面是我试图解析的html,下面是我使用的代码。。如果可能的话,还可以发布一个解决方案,将它们全部作为单个数组拉入

<div class="column-block" id="hematology">
<h3 class="panel-title names strong">
<a class="speciality" rel="hematology" href="https://www.lyfboat.com/hospitals/hematology-hospitals-and-costs/">
Hematology </a>
</h3>
<div class="links">
<a target="_blank" href="https://www.lyfboat.com/procedures/allogenic/">Allogenic Bone Marrow Transplant</a><a target="_blank" href="https://www.lyfboat.com/aplastic-anemia-treatment-in-india/">Aplastic Anemia</a><a target="_blank" href="https://www.lyfboat.com/procedures/autologous-for-multiple-lymphomas/">Autologous Bone Marrow Transplant</a><a target="_blank" href="https://www.lyfboat.com/blood-cancer-treatment-hospitals-costs-in-india/">Blood Cancer Treatment</a><a target="_blank" href="https://www.lyfboat.com/bone-marrow-transplant-hospitals-costs-india/">Bone Marrow Transplant (BMT)</a><a target="_blank" href="https://www.lyfboat.com/fanconis-anemia-treatment-in-india/">Fanconi Anemia</a><a target="_blank" href="https://www.lyfboat.com/leukemia-treatment-cost-hospitals-surgeons-in-india/">Leukemia Treatment</a><a target="_blank" href="https://www.lyfboat.com/lymphoma-treatment-costs-hospitals-surgeons-in-india/">Lymphoma Treatment</a><a target="_blank" href="https://www.lyfboat.com/multiple-sclerosis-treatment-in-india/">Multiple Sclerosis</a><a target="_blank" href="https://www.lyfboat.com/hospitals/myeloma-blood-cancer-hospitals-and-costs/">Myeloma Treatment</a><a target="_blank" href="https://www.lyfboat.com/hospitals/pediatric-bone-marrow-transplant-hospitals-and-costs/">Pediatric Bone Marrow Transplant</a><a target="_blank" href="https://www.lyfboat.com/sickle-cell-anemia-treatment-in-india/">Sickle Cell Disease</a><a target="_blank" href="https://www.lyfboat.com/hospitals/thalassemia-transplant-hospitals-and-costs/">Thalassemia Transplant</a> </div>
</div>
<div class="column-block" id="pediatric-cardiology">
<h3 class="panel-title names strong">
<a class="speciality" rel="pediatric-cardiology" href="https://www.lyfboat.com/hospitals/pediatric-cardiology-hospitals-and-costs/">
Pediatric Cardiology </a>
</h3>
<div class="links">
<a target="_blank" href="https://www.lyfboat.com/hospitals/arterial-switch-operation-truncus-arteriosis-hospitals-and-costs/">Arterial switch operation/ Truncus arteriosis</a><a target="_blank" href="https://www.lyfboat.com/asd-closure-cost-surgeon-hospitals-in-india/">Atrial Septal Defect Closure (ASD)</a><a target="_blank" href="https://www.lyfboat.com/hospitals/atrioventricular-canal-defect-av-canal-hospitals-and-costs/">Atrioventricular Canal Defect</a><a target="_blank" href="https://www.lyfboat.com/hospitals/balloon-atrial-septostomy-hospitals-and-costs/">Balloon Atrial Septostomy</a><a target="_blank" href="https://www.lyfboat.com/hospitals/double-outlet-right-ventricle-dorv-hospitals-and-costs/">Double Outlet Right Ventricle</a><a target="_blank" href="https://www.lyfboat.com/hospitals/fontan-hospitals-and-costs/">Fontan</a><a target="_blank" href="https://www.lyfboat.com/hospitals/glenn-hospitals-and-costs/">Glenn Procedure</a><a target="_blank" href="https://www.lyfboat.com/procedures/patent-ductus-arteriosus-pda-device-closure/">Patent Ductus Arteriosus Device Closure Catheterization</a><a target="_blank" href="https://www.lyfboat.com/fallots-tetralogy-treatment-cost-hospitals-in-india/">Tetralogy of Fallot</a><a target="_blank" href="https://www.lyfboat.com/hospitals/total-anomalous-pulmonary-venous-connection-tapvc-hospitals-and-costs/">Total Anomalous Pulmonary Venous Connection</a><a target="_blank" href="https://www.lyfboat.com/hospitals/transposition-of-the-great-arteries-tga-hospitals-and-costs/">Transposition of the Great Arteries (TGA)</a><a target="_blank" href="https://www.lyfboat.com/hospitals/valvuplasty-hospitals-and-costs/">Valvuplasty</a> </div>
</div>```


替换:

for department in containers:
    if(department.find('a')):
      data['Department'].append(department.find('a', {'class': 'speciality'}).text)
      data['Conditions'].append(department.find('div', {'class': 'links'}).text)[0:]
for department in containers:
    if(department.find('a')):
        data['Department'].append(department.find('a', {'class': 'speciality'}).text)
        links = department.find('div', {'class': 'links'})
        for link in links.find_all("a"):
             data['Conditions'].append(link.get_text())
与:

for department in containers:
    if(department.find('a')):
      data['Department'].append(department.find('a', {'class': 'speciality'}).text)
      data['Conditions'].append(department.find('div', {'class': 'links'}).text)[0:]
for department in containers:
    if(department.find('a')):
        data['Department'].append(department.find('a', {'class': 'speciality'}).text)
        links = department.find('div', {'class': 'links'})
        for link in links.find_all("a"):
             data['Conditions'].append(link.get_text())

请加上你对未来的期望result@GiovaniSalazar一个单独的列,其中
h3>a class=“speciality”
中的项目作为标题,在该标题下,
div class=“links”
中的项目作为新行中的单独项目。我不能把它们放在一个新的行中,因为我不知道在split中用什么作为分隔符()啊,非常感谢兄弟。。。我不知道这个函数get_text(),抱歉,我正在学习。。难道没有人在SOF toobro中使用它,而不是将它们作为两个单独的数组取出吗?将它们全部作为单个数组取出的逻辑是什么。。。我必须把它作为两个独立的数组,就像我的repl-it一样