python - 多次发现后出现美丽的soup.find错误
问题描述
我尝试用漂亮的汤编辑一个 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'
有任何想法吗?如果我的错误很简单,请原谅我,编码只是一种爱好,我几乎不知道。谢谢!
解决方案
推荐阅读
- c# - 泛型中的接口约束
- pseudocode - 如何在伪代码中使用while循环
- python-3.x - 在配置中调用 hydra.utils.instantiate
- flutter - 在 null 上调用了 getter 'length'。Receiver null 尝试调用 'length' list.bilder
- python - 将 python 脚本中的值获取到 powershell 脚本中
- r - R Shiny --> 有条件地突出显示条形图
- sql - 通过 MS Access 中的查询将数据类型更改为日期?
- c# - 棘手的继承相关架构
- ios - Xamarin 打开 WiFi 热点/接入点并获取网络名称和密码
- android - 为什么我的功能不起作用但在 Logcat (Android studio) 中显示错误和输出?