Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在文本后搜索BeautifulSoup,需要从表行中获取所有数据_Python_Beautifulsoup - Fatal编程技术网

Python 在文本后搜索BeautifulSoup,需要从表行中获取所有数据

Python 在文本后搜索BeautifulSoup,需要从表行中获取所有数据,python,beautifulsoup,Python,Beautifulsoup,我有一张这样的桌子: <table id="test" class="tablesorter"> <tr class="even"> <td style="background: #F5645C; color: #F5645C;">1&#160;</td> <td>Major Lazer</td> <td class="right">64</td> <td>93.1

我有一张这样的桌子:

<table id="test" class="tablesorter">
<tr class="even">
  <td style="background: #F5645C; color: #F5645C;">1&#160;</td>
  <td>Major Lazer</td>
  <td class="right">64</td>
  <td>93.1.15.107</td>
  <td>0x0110000105DAB310</td>
  <td class="center">No</td>
  <td class="center">No</td>
</tr>

<tr class="odd">
  <td style="background: #8FB9B0; color: #8FB9B0;">0&#160;</td>
  <td>Michael gunin</td>
  <td class="right">64</td>
  <td>57.48.41.27</td>
  <td>0x0110000631HDA213</td>
  <td class="center">No</td>
  <td class="center">No</td>
</tr>

...

</table>

这向我展示了td,但我不知道从这里该做什么。

一种方法是使用
查找以前的兄弟姐妹(“td”)

Ex:

for tr in table_rows:
    td = tr.find('td', text='0x0110000105DAB310')
    if td is not None:
        print( td.find_previous_sibling('td').text )
        print( td.find_previous_sibling('td').find_previous_sibling('td').find_previous_sibling('td').text )

将所有表格数据保存在pandas dataframe中,与您正在遵循的方法相比,将更容易获取数据。您可以做的是搜索该特定单词,然后如果您在该特定
tr
上找到该单词,则只需使用
td=tr.find获取该文本的父标记('td',text='0x0110000101517CC6')。父级
然后您可以简单地将文本按
td
拆分,并访问要存储其数据的索引号。太棒了!我可以从这里进一步了解,谢谢!
for tr in table_rows:
    td = tr.find('td', text='0x0110000105DAB310')
    if td is not None:
        print( td.find_previous_sibling('td').text )
        print( td.find_previous_sibling('td').find_previous_sibling('td').find_previous_sibling('td').text )