Python 漂亮的汤。在几次发现后发现错误

Python 漂亮的汤。在几次发现后发现错误,python,html,beautifulsoup,Python,Html,Beautifulsoup,我试着用BeautifulSoup编辑一个html文件。在开始时遇到了一些问题之后,我想我终于完成了。但是现在我在我的一个循环中得到了一个随机错误,我真的不明白。我执行了一些检查,也许可以找到错误,但这对我没有帮助。我怀疑某种“过载”,但不知道如何修复它 from bs4 import BeautifulSoup html_file = '****' #html file location with open(html_file, "r") as file_content: dat

我试着用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
从bs4导入美化组
html_文件='***'#html文件位置
打开(html_文件,“r”)作为文件内容:
数据=文件内容。读取()
soup=BeautifulSoup(数据'html.parser')
x=2
y=1
打印(soup.find(id=“2-1”))
old_line=soup.find(id=f“{x}-{y}”).parent
打印(旧线)
x=1
当x<10时:
y=1
y<6时:
打印(f“x={x}”)
打印(f“y={y}”)
打印(f“{x}-{y}”)
打印(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}”)
新行['id']=f“{x}-{y}”
旧的_行。查找(“td”,id=f“{x}-{y}”)。将_替换为(新的_行)
打印(旧线)
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'

x=1
y=1
1-1
x=1
y=2
1-2
x=1
y=3
1-3
x=1
y=4
1-4
x=1
y=5
1-5
scrotiemcboogerballs greift和verursacht 12345678 Schaden
格里夫特生物学院
x=2
y=1
2-1
没有一个
回溯(最近一次呼叫最后一次):
文件“****”,第25行,在
old_line=soup.find(“td”,id=f“{x}-{y}”).parent
AttributeError:“非类型”对象没有属性“父”

有什么想法吗?如果我的错误很简单,请原谅我,编码只是一种爱好,我几乎不知道。谢谢大家!

如果您所做的只是在每次迭代结束时以相同的数量递增
x
,那么在x<10:时使用类似
的循环是没有意义的。只需使用for循环和范围。你能提供一份工作吗?以下文章应该会派上用场:。感谢您的输入!我想我找到了我的错。我在循环中编辑了id 2-1,并将其id更改为1-1。这就是为什么我在开始时找到了id 2-1,但在第一次编辑循环之后没有找到。考虑到这一点,我重新编写了代码,它可以正常工作。用你提到的循环。我考虑了一个“for x in range(len())”循环,但选择了while循环。到底有什么区别?效果是一样的,不是吗?感谢您的调试页面!