Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 extract转换为sqlite表_Python_Sqlite - Fatal编程技术网

Python 将BeautifulSoup extract转换为sqlite表

Python 将BeautifulSoup extract转换为sqlite表,python,sqlite,Python,Sqlite,我有一个html表结构,看起来像: <table> <tbody> <tr> <td> <ul> </ul </td> <td> <table> <tbody&

我有一个html表结构,看起来像:

<table>
    <tbody>
        <tr>
            <td>
                <ul>
                </ul
            </td>
            <td>
                <table>
                    <tbody>
                        <tr></tr>
                        <tr></tr>
                        <tr></tr>
                    </tbody>
                </table>
                <table>  -- (table structure I am interested in)
                    <tbody>
                        <tr>
                            <td class="dte"></td>
                            <td class="id"></td>
                            <td class="desc"></td>
                        </tr>
                        <tr>
                            <td class="dte"></td>
                            <td class="id"></td>
                            <td class="desc"></td>
                        </tr>
                        <tr>
                            <td class="dte"></td>
                            <td class="id"></td>
                            <td class="desc"></td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>
使用以下代码(我从本网站的各种帖子中破解):-

2个问题:

  • 当输出打印到屏幕上时,我在第一行得到整个表结构(因此上面的每个示例都将在第一行输出(实际表的长度约为100个条目)),然后从第二行得到每行一个条目(如上所示),这就是我想要的。如何忽略或不输出第一行的完整结构?为什么我会这样
  • 我想将上面显示的结果转换为sqlite3表结构,稍后我将etl转换为生产mssql环境。根据我得到的输出,我还没有找到一种方法来实现这一点

  • 你看过报纸了吗?还有,为什么要将编码改为bytestring?很抱歉这么晚才回来。。。我已经设法把它分类了。如果有人感兴趣,我可以详细介绍,但结果是我从许多来源中拖入了数据,这是许多来源中的一种结构。。。但是如果需要的话,我可以回溯并计算出它的用途,并提供我提出的解决方案。你读过这篇文章了吗?还有,为什么要将编码改为bytestring?很抱歉这么晚才回来。。。我已经设法把它分类了。如果有人感兴趣,我可以详细介绍,但结果是我从许多来源中拖入了数据,这是许多来源中的一种结构。。。但如果需要的话,我可以回溯并计算出它的用途,并提供我提出的解决方案。
    [b'16 March', b'987654', b'Something happens on this date']
    [b'23 March', b'321987', b'Something happens on this date']
    [b'26 March', b'123456', b'Something happens on this date']
    
    for mytable in soup.find('body').find_all('table'):
            #print (len(mytable))
            for trs in mytable.find_all('tr'):
                tds = trs.find_all('td', class_='dte id desc'.split())
    
                if tds: # checks if 'tds' has value.  if YES then block is executed
                    row = [elem.text.strip().encode('utf-8') for elem in tds]
                    print (row)
    
                else:
                    continue # 'row' item is empty, proceed to next loop