当CSS表是JSON对象时,如何使用BeautifulSoup导入该表?

2021-12-08 05:13:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从一个使用BS4的CSS繁重的url导入一个表。这是我的密码:

import csv
from bs4 import BeautifulSoup
import urllib.request as ur

outfile = open(r"table_data.csv","w+",newline='')
writer = csv.writer(outfile)

html = ur.urlopen('url')

tree = BeautifulSoup(html,"lxml")

table_tag = tree.select(playersData)[0]
tab_data = [[item.text for item in row_data.select("th,td")]
                for row_data in table_tag.select("tr")]

for data in tab_data:
    writer.writerow(data)
    print(' '.join(data))

tree变量的相关部分如下所示:

<div class="block">
<div class="block-content">
<div class="players" data-countinpage="10" data-pagination="true" id="league-players"></div>
<script>
        var playersData = JSON.parse('\x5B\x7B\x22id\x22\x3A\x221250\x22,\x22player_name\x22\x3A\x22Mohamed\x20Salah\x22,\x22games\x22\x3A\x2235\x22,\x22time\x22\x3A\x222869\x227D');
</script> </div>
</div>

如何将playersData导入csv?你知道吗