简体   繁体   English

无论如何我可以获得img标签的position吗? 在 python 中使用 beautifulsoup 找到 function

[英]Is there anyway I can get the position of img tag? In python using beautifulsoup find function

I would like to know at what position of the td tag the.png image file lies.我想知道 td 标签的.png 图像文件的 position 位于什么位置。

For example, in this case (row 5) is there any way I can get the position of the image tag?例如,在这种情况下(第 5 行),我有什么办法可以获得图像标签的 position? In python using beautifulsoup to find the function or scrappy?在 python 中使用 beautifulsoup 找到 function 还是 scrappy?

</tr>
            <tr id="table_1_row_3">
                <td style="">A List</td>
                                                    <td style=""></td>
                                                    <td style="">Itemnumber</td>
                                                    <td style="">price</td>
                                                    <td style=""></td>
                                                    <td style=""><img src='https://www.example.com/.png' /></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style="">min price<BR>a List<BR></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    <td style=""></td>
                                                    

You can use built-in function enumerate() for the task:您可以使用内置的 function enumerate()来完成任务:

from bs4 import BeautifulSoup


txt = '''
    <tr id="table_1_row_3">
        <td style="">A List</td>
        <td style=""></td>
        <td style="">Itemnumber</td>
        <td style="">price</td>
        <td style=""></td>
        <td style=""><img src='https://www.example.com/.png' /></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style="">min price<BR>a List<BR></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
        <td style=""></td>
    </tr>'''


soup = BeautifulSoup(txt, 'html.parser')

# find all positions of <img> inside <tr>:
idx = [i for i, td in enumerate(soup.select('tr > td')) if td.img]

# print indexes, where there is <img> inside <td>:
print(idx)

Prints:印刷:

[5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM