如何在Django中使用bs4查找tbody

 收藏

任何帮助将不胜感激,我正在尝试使用django在bs4中的表内找到标记,该标记在控制台上显示结果,但在vps服务器上不起作用。虽然当我在本地主机上尝试相同的代码时,它仍在工作。

当我尝试访问bs4内的标签时,它显示Nonetype Error,但相同的代码在localhost上运行正常

我在服务器和本地主机上都具有相同版本的python

这是我的代码在服务器上有问题

if soup.find('tbody').find_all('tr') is not None: #but here it gives us None_type tbody tag #soup not finding tbody tag

这是完整的代码

soup = BeautifulSoup(sorted_df.to_html(), "html.parser")
        count = 0
        for j in soup.find("thead").find_all('tr'):
            if count == 0:
                j.append(BeautifulSoup('<td><b><center>Next Result</center></b></td>', 'html.parser'))
                count += 1
            else:
                j.append(BeautifulSoup('<td></td>', 'html.parser'))


        modal_identifier = 0
        print(soup,"HERE") #here it shows that soup has tbody tag in it
        job_card = 0
        if soup.find('tbody').find_all('tr') is not None: #but here it gives us None_type tbody tag #soup not finding tbody tag
            i = 0
            while ((soup.find_all("tr")) is not None) and i < len(soup.find_all('tr')):
                temp = soup.find_all('tr')[i].find('th').text
                print(temp, "TEMP")
                soup.find_all('tr')[i].append(BeautifulSoup(
                    '<td><button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal' + str(
                    modal_identifier) + '">Stations Detail</button><div class="modal fade" id="myModal' + str(
                    modal_identifier) + '" role="dialog"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button><h4 class="modal-title">Modal Header</h4></div><div class="modal-body"><h4>All Station & Timing</h4><p>' + str(
                    testing2[int(temp)]) + '</p><p>' + str(
                    testing3[int(temp)]) + '</p><p>' '</p><h4>Number Of Service Station</h4><p>' + str(
                    numberOfServiceStation[int(temp)]) + '</p><h4>Service Station Name & Timing</h4><p>' + str(
                    stationName[str(temp)]) + '</p><p>' + str(stationTime[int(
                    temp)]) + '</p></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div></div></div></div></td>',
                'html.parser'))
                i += 1
                modal_identifier += 1
                job_card += 1

结果显示在控制台上

Console Results

并在网页上显示错误

enter image description here

回复