如何在Python中动态地将HTML写入文件



我有以下代码:

for item in soup.select('.profile-detail'):
f= open('htmlfile.html', 'w')
f.write(item)
f.close()

我想把项目写入"htmlfile.html",但它给了我错误:

TypeError:write()参数必须是str,而不是Tag

只需使用str()即可获得整个标签内容:

with open('htmlfile.html', 'w') as f:
for item in soup.select('.profile-detail'):
f.write(str(item) + 'n') # thanks jeteon :p, it's cleaner

我从您剪辑的soup中了解到,您正在使用BeautifulSoup提取具有类"profile detail"的元素。考虑到这一点,您的代码有两个问题:

  1. select()函数返回的列表中的值item是Tag类的实例,文件对象的write方法需要一个字符串。正如@PRMoureu所写,您可以将Tag实例强制转换为一个字符串,它将通过用以下内容替换文件写入行来返回它所代表的原始HTML字符串:

    f.write(str(item))
    
  2. 您打开的文件在循环中以写("w")模式打开。这意味着,对于循环的每次迭代,文件都将被覆盖,如果您试图收集查询返回的所有元素,则只能获得最后一个元素。如果你想把它们都放在文件中,以下是修复它的替代方法:

    # Open the file in append mode
    for item in soup.select('.profile-detail'):
    f = open('htmlfile.html', 'a')
    f.write(item)
    f.close()
    

    但这并不太好,因为没有必要在每次迭代中打开和关闭文件。我们可以为所有写入保持文件打开:

    # Open and close file just once
    f = open('htmlfile.html', 'w')            # Okay to open in write mode now
    for item in soup.select('.profile-detail'):
    f.write(item)
    f.write('n') # In case you're expecting each on a new line
    f.close()
    

    或者,我个人最喜欢的是,做几乎相同的事情,但有上下文,这样你就不用担心忘记f.close(),或者后来不小心给了它错误的缩进或其他什么:

    # With contexts
    with open('htmlfile.html', 'w') as f:
    for item in soup.select('.profile-detail'):
    f.write(item)
    f.write('n') # In case you're expecting each on a new line
    # File is auto-magically closed by the time you get here
    

最新更新