如何在不使用循环的情况下获得python beautifulsoup中的特定部分

2024-07-08 16:34:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码:

page = requests.get("http://www.freejobalert.com/upsc-recruitment/16960/#Engg-Services2019")
c = page.content
soup=BeautifulSoup(c,"html.parser")
tables=soup.find_all("table",{"style":"width: 500px;"})

Html表格:

<table style="width: 500px;" border="2">
<tbody>
<tr>
<td colspan="2">
<p style="text-align: center;"><span style="color: #ff0000;"><strong>Union Public Service Commission (UPSC)</strong></span></p>
<p style="text-align: center;"><span style="color: #ff00ff;"><strong>Advt No.01/2019</strong></span></p>
<p style="text-align: center;"><span style="color: #008000;"><strong><strong><strong>Engineering Services (Prelims) Exam 2019</strong></strong></strong></span></p>
<p style="text-align: center;"><strong><a href="http://www.freejobalert.com" target="_blank">WWW.FREEJOBALERT.COM</a></strong></p>
</td>
</tr>
<tr>
<td style="text-align: center;" colspan="2"><span style="color: #ff0000;"><strong>Application Fee</strong></span></p>
<ul>
<li style="text-align: left;"><span style="line-height: 19px;">For Female/SC/ST/ PH: <strong>NIL</strong></span></li>
<li style="text-align: left;"><span style="line-height: 19px;">For Others: <strong>Rs. 200/-</strong></span></li>
<li style="text-align: left;">Candidates can pay either by depositing the money in any Branch of SBI by cash or by using net banking facility of SBI.</li>
</ul>
</td>
</tr>
<tr>
<td style="text-align: center;" colspan="2"><span style="color: #ff0000;"><strong><strong>Important Dates</strong></strong></span></p>
<ul>
<li style="text-align: left;">Starting Date to Apply Online: <strong>26-09-2018</strong></li>
<li style="text-align: left;"><span style="line-height: 19px;">Last Date to Apply Online: <strong>22-10-2018 till 06:00 PM</strong></span></li>
<li style="text-align: left;"><span style="line-height: 19px;">Date for Preliminary Exam:<strong> 06-01-2019</strong></span></li>
<li style="text-align: left;"><span style="line-height: 19px;">Last date for Fee Payment (Pay by cash):<strong> 21-10-2018 at 11.59 PM</strong></span></li>
<li style="text-align: left;"><span style="line-height: 19px;">Last date for Fee Payment (online):<strong> 22-10-201</strong></span><strong>8 till 06:00 PM</strong></li>
</ul>
</td>
</tr>
</tbody>
</table>

我期待着:

[
    <td colspan="2">
    <p style="text-align: center;"><span style="color: #ff0000;"><strong>Union Public Service Commission (UPSC)</strong></span></p>
    <p style="text-align: center;"><span style="color: #ff00ff;"><strong>Advt No.01/2019</strong></span></p>
    <p style="text-align: center;"><span style="color: #008000;"><strong><strong><strong>Engineering Services (Prelims) Exam 2019</strong></strong></strong></span></p>
    <p style="text-align: center;"><strong><a href="http://www.freejobalert.com" target="_blank">WWW.FREEJOBALERT.COM</a></strong></p>
    </td>
]

我如何才能得到这些部分的html使用美丽的汤没有使用循环。。 我想要所有的colspan=“2”

请检查一下这个代码

谢谢


Tags: textstylelinelilefttrcolortd
1条回答
网友
1楼 · 发布于 2024-07-08 16:34:55

是什么阻止你直接找到这些元素

from bs4 import BeautifulSoup
import requests
page = requests.get("http://www.freejobalert.com/upsc-recruitment/"
"16960/#Engg-Services2019")
c = page.content
soup = BeautifulSoup(c,"html.parser")
tables = soup.find_all("td", attrs={'colspan':'2'})
print(tables)

相关问题 更多 >

    热门问题