Прекрасная ошибка - PullRequest
       0

Прекрасная ошибка

0 голосов
/ 19 января 2020

Я пытаюсь отредактировать html -файл с красивым супом. После нескольких проблем со стартом я подумал, что наконец сделал это. Но теперь я получаю случайную ошибку в одном из моих циклов, которую я действительно не понимаю. Я реализовал несколько проверок, чтобы, возможно, найти ошибку, но она мне не помогла. Я подозреваю какую-то «перегрузку», но не могу понять, как я могу это исправить.

from bs4 import BeautifulSoup


html_file = '****' #html file location

with open(html_file, "r") as file_content:
    data = file_content.read()
soup = BeautifulSoup(data, 'html.parser')

x = 2
y = 1
print(soup.find(id="2-1"))
old_line = soup.find(id=f"{x}-{y}").parent
print(old_line)

x = 1
while x < 10:
    y = 1
    while y < 6:
        print(f"x={x}")
        print(f"y={y}")
        print(f"{x}-{y}")

        print(soup.find("td", id=f"{x}-{y}"))
        old_line = soup.find("td", id=f"{x}-{y}").parent
        new_line = soup.find("td", id=f"{x+1}-{y}")
        new_line['id'] = f"{x}-{y}"
        old_line.find("td", id=f"{x}-{y}").replace_with(new_line)
        print(old_line)
        y += 1
    x += 1

выход:

<td class="kleine-tabelle" id="2-1"><img height="40" src="tumbleweed.gif" width="40"/></td>
<tr>
<td class="kleine-tabelle" id="2-1"><img height="40" src="tumbleweed.gif" width="40"/></td>
<td class="kleine-tabelle" id="2-2"><img height="40" src="tumbleweed.gif" width="40"/></td>
</tr>
x=1
y=1
1-1
<td class="kleine-tabelle" id="1-1"><img height="40" src="tumbleweed.gif" width="40"/></td>
<tr>
<td class="kleine-tabelle" id="1-1"><img height="40" src="tumbleweed.gif" width="40"/></td>
<td class="kleine-tabelle" id="1-2"><img height="40" src="tumbleweed.gif" width="40"/></td>
</tr>
x=1
y=2
1-2
<td class="kleine-tabelle" id="1-2"><img height="40" src="tumbleweed.gif" width="40"/></td>
<tr>
<td class="kleine-tabelle" id="1-1"><img height="40" src="tumbleweed.gif" width="40"/></td>
<td class="kleine-tabelle" id="1-2"><img height="40" src="tumbleweed.gif" width="40"/></td>
</tr>
x=1
y=3
1-3
<td class="kleine-tabelle" id="1-3"><img height="40" src="tumbleweed.gif" width="40"/></td>
<tr>
<td class="kleine-tabelle" id="1-3"><img height="40" src="tumbleweed.gif" width="40"/></td>
<td class="kleine-tabelle" id="1-4"><img height="40" src="tumbleweed.gif" width="40"/></td>
</tr>
x=1
y=4
1-4
<td class="kleine-tabelle" id="1-4"><img height="40" src="tumbleweed.gif" width="40"/></td>
<tr>
<td class="kleine-tabelle" id="1-3"><img height="40" src="tumbleweed.gif" width="40"/></td>
<td class="kleine-tabelle" id="1-4"><img height="40" src="tumbleweed.gif" width="40"/></td>
</tr>
x=1
y=5
1-5
<td id="1-5"><span class="player-text">scrotiemcboogerballs</span> greift an<br/>Er verursacht <span class="red-text">12345678</span> Schaden</td>
<tr style="border:0px black none; margin: 0px; padding: 0px">
<td class="grafik-spalte">
<table>
<tr>
<td class="kleine-tabelle" id="1-1"><img height="40" src="tumbleweed.gif" width="40"/></td>
<td class="kleine-tabelle" id="1-2"><img height="40" src="tumbleweed.gif" width="40"/></td>
</tr>
<tr>
<td class="kleine-tabelle" id="1-3"><img height="40" src="tumbleweed.gif" width="40"/></td>
<td class="kleine-tabelle" id="1-4"><img height="40" src="tumbleweed.gif" width="40"/></td>
</tr>
</table>
</td>
<td id="1-5"><span class="enemy-text">Biosellerie</span> greift an<br/>Er verursacht <span class="red-text">5743841</span> Schaden</td>
</tr>
x=2
y=1
2-1
None
Traceback (most recent call last):
  File "****", line 25, in <module>
    old_line = soup.find("td", id=f"{x}-{y}").parent
AttributeError: 'NoneType' object has no attribute 'parent'

есть идеи? пожалуйста, прости меня, если моя ошибка проста, кодирование - это просто хобби, и я почти не знаю. Спасибо!

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