我试着用BeautifulSoup编辑一个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'
有什么想法吗?如果我的错误很简单,请原谅我,编码只是一种爱好,我几乎不知道。谢谢大家!
目前没有回答
相关问题 更多 >
编程相关推荐