BeautifulSoup 修改元素的值 value

创建日期: 2023-08-16 18:33 | 作者: 风波 | 浏览次数: 13 | 分类: Python

来源:https://stackoverflow.com/questions/9766966/how-to-set-value-in-with-beautiful-soup-in-some-html-element-if-i-know-id-of-tha

代码

from bs4 import BeautifulSoup

soup = None
with open('abc.html', 'rb') as f:
    soup = BeautifulSoup(f.read(), "html.parser" )

t = soup.find("title")
t.string = 'Hello World'

html = soup.prettify() # 获取格式化后的 html 文本

安装 BeautifulSoup

pip install BeautifulSoup4
13 浏览
10 爬虫
0 评论