-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratch_13.py
More file actions
34 lines (30 loc) · 1.27 KB
/
Copy pathscratch_13.py
File metadata and controls
34 lines (30 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from bs4 import BeautifulSoup
# 读取HTML文件内容
with open(r'./gwent.html', 'r', encoding='utf-8') as file:
html_content = file.read()
# 使用BeautifulSoup解析HTML内容
soup = BeautifulSoup(html_content, 'html.parser')
updated_html_content = ""
# 找到所有的card-wrap card-data元素
card_elements = soup.find_all("div", class_="card-wrap")
# print(card_elements)
# 遍历这些元素
for card in card_elements:
# 提取data-faction属性的值
faction = card.get('data-faction')
provision = card.get('data-provision')
# print(faction)
if faction: # 确保data-faction属性存在
# 创建一个新的span元素来显示faction值
faction_span = soup.new_tag('span')
faction_span.string = f'阵营: {faction}'
provision_span = soup.new_tag('span')
provision_span.string = f'人口: {provision}'
# 找到card-info-wrap的最后一个子元素,然后将其插入到该位置
card.insert(1, provision_span)
if faction == "neutral" or faction == "northern_realms":
updated_html_content += str(card)
# 将修改后的soup对象转换为字符串
# 保存更新后的HTML内容到新文件
with open(r'./output.html', 'w', encoding='utf-8') as file:
file.write(updated_html_content)