Python 将BeautifulSoup extract转换为sqlite表
我有一个html表结构,看起来像:Python 将BeautifulSoup extract转换为sqlite表,python,sqlite,Python,Sqlite,我有一个html表结构,看起来像: <table> <tbody> <tr> <td> <ul> </ul </td> <td> <table> <tbody&
<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个问题:
你看过报纸了吗?还有,为什么要将编码改为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