如何获得包裹在表中的标签值?
<td> <label for="cp_designation">Designation : </label></td>
<td> PARTNER</td>
</tr>
<tr>
<td><label for="cp_category">Category : </label></td>
<td>SPORTS GEARS</td>
</tr>
<tr>
<td> <label for="cp_address">Address : </label></td>
<td> A-148, WARD NO.4, PAINTER STREETSIALKOT-CANTT.</td>
</tr>
<tr>
<td> <label for="cp_phone">Phone : </label></td>
<td> 4603886,</td>
</tr>
soup = bs(page.content, "html.parser")
for i in soup:
label = soup.find_all('label',text='Designation : ')
print(label.find('tr'))
大家好,我的问题是我想提取标签中的标签值,我尝试了很多东西,但没有获得价值。你们有没有任何专业,如果是的,那么这将是可喜的。提前致谢。
<td> <label for="cp_designation">Designation : </label></td>
<td> PARTNER</td>
</tr>
<tr>
<td><label for="cp_category">Category : </label></td>
<td>SPORTS GEARS</td>
</tr>
<tr>
<td> <label for="cp_address">Address : </label></td>
<td> A-148, WARD NO.4, PAINTER STREETSIALKOT-CANTT.</td>
</tr>
<tr>
<td> <label for="cp_phone">Phone : </label></td>
<td> 4603886,</td>
</tr>
soup = bs(page.content, "html.parser")
for i in soup:
label = soup.find_all('label',text='Designation : ')
print(label.find('tr'))
hi y'all my question is that i want to extract label value that is in tag i tried so many things but fail to get value. did you guys has any experties if yes so it would be hightly appreciatable. thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里,您可以在
find_all
的方法上找到主TR
标记通过标签
标记将数据作为key-value
获取数据。配对并使用find_next
使用标签
标签获得下一个标签,以获取标签值输出的值:
Here you can find main
tr
tag withfind_all
method to iterate overlabel
tag to get data askey-value
pair and usefind_next
to get next tag withlabel
tag to get values of labelsOutput:
我们在这里要做的是列出标题列表,列出表行列表,然后将标题划为表数据标签中存储的数据(作为文本),然后我们将其转换为词典,然后添加到列表中。
这不是刮擦的最佳方法,因为您可以点击不存在数据的问题,并且在不正确的位置中进行数据,但是,您可以将其调整为更强大。
What we do here is take a list of the headers, take a list of the table rows, and zip the headers to the data stored in the table data tag (as text), we then convert this to a dictionary and add to a list.
This isn't the best way of scraping as you can hit issues where data doesn't exist and data in the incorrect location, however with the below you can adapt it to be more robust.