Как применить find.all () с помощью bs4 в Django - PullRequest
0 голосов
/ 09 января 2020

Я преобразовываю фрейм данных в таблицу, после чего я пытаюсь выяснить <tr></tr> внутри тега таблицы, проблема заключается в том, что <tr></tr> внутри тела таблицы. Хотя тот же код работает на локальном хосте, но при развертывании он создает проблемы с тем, что на него это не распространяется. Когда я пытаюсь получить доступ к <tr></tr> внутри заголовка таблицы, он работает отлично, но не в теле таблицы.

Здесь, внутри find ("thead"). Find_all ('tr' ) он работает здесь, в этой части кода

 for j in soup.find("thead").find_all('tr'):

Но когда я пытаюсь получить доступ

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

Это дает us None_type тег tbody #soup не находит тег tbody

Это полный код

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

Тот же код работает на локальном хосте, но не на облачном сервере

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...