我有以下代码,从md文件加载所有td并搜索特定的合作伙伴。部分代码如下所示
html_string = html.replace(' ', '')
soup = BeautifulSoup(html_string)
tds = soup.findAll("td", {"class":"confluenceTd"}) # Find all td tags
temp_holder={} # Get a temp dictionary
html_as_list = list(html_string)
html_join = "".join(html_as_list)
prev_tag = []
for td_name in tds:
...code ommited...
...code ommited...
#print("TD name: ",td_name)
tag_index = '<td colspan="1" class="confluenceTd">'
end_tag = '</td>'
get_tag_value = str(td_name.text.strip())
get_text_index = html_string.index(get_tag_value) # Get the index which text start
position_f = get_text_index - len(tag_index) # Get the index of the first character of the line "**<**" in our case
full_tag = tag_index + get_tag_value + end_tag # Will print <td colspan="1" class="confluenceTd">DDD-3103</td>
#print(html_string.index(str(position_f)))
result1 = search_j(get_tag_value) # Get the text from p tag and check if it has jira issue
if result1 == -1:
continue
az_id = str(query_a(result1))
res1 = re.sub(result1, " #"+az_id+" ", full_tag) # Will return <td colspan="1" class="confluenceTd"> #11111 </td>
#print("Add key: ", position_f, " values: ",res1)
# Check if there is any duplicate value
# If douplicate exists get the position of in the original list
if get_tag_value in prev_tag:
dup = list_duplicates_of(html_join, get_tag_value)
for dupl in dup:
position_s = dupl - len(tag_index)
temp_holder[position_s] = res1
else:
temp_holder[position_f] = res1
#print(res1)
prev_tag.append(get_tag_value)
for html_keys, html_values in temp_holder.items(): #Replace the old line with the new one
#print(html_keys + len(html_values))
#sys.exit()
html_as_list[html_keys] = html_values
print("P: ",html_keys, "V: ",html_values)
html_fin = "".join(html_as_list)
return html_fin
filename = 'PoPs.md'
with open(filename, "r") as f:
html_string = f.read()
result = check_td(html_string)
save_filename="test.md"
#
w = open (save_filename, "a")
w.write(str(result))
w.close()
我使用一个临时字典来保存更新值,如下所示 关键:在我们的例子中,第一个字符的位置是“<;”吗 值:是更新值
印刷品将显示:
P: 2651 V: "<td colspan="1" class="confluenceTd"> #11111 </td>"
最终结果保存在下面的文件中
...output ommited...
<td colspan="1" class="confluenceTd"> #11111 </td>td colspan="1" class="confluenceTd">DDD-3103</td>
...output ommited...
如您所见,只需更换<而不是整行
我希望替换
"<td colspan="1" class="confluenceTd">DDD-3103</td>" with "<td colspan="1" class="confluenceTd"> #11111 </td>"
为了使其正常工作,我可能缺少什么
有什么想法吗
所有的问题都是因为你期望太高
使用
html_as_list = list(html_string)
可以创建如下字符列表当您使用
html_as_list[html_keys] = ...
时,您将替换此列表中的单个元素,即单个字符您可以尝试使用slice
html_as_list[html_keys:position_f] = list(text)
,但如果您放置更长/更短的文本,它将改变列表的大小,并将改变其他元素的位置您可以尝试使用普通的
text.replace()
,但它也会更改字符串的长度,并且下一个元素将位于不同的位置,所以您必须在搜索下一个元素之前替换它如果要替换HTML中的文本或标记,只需使用
BeautifulSoup
即可函数
find_all
(和其他函数)提供对HTML树中元素的引用,以便可以更改原始HTML中的值顺便说一句:它必须是
.string
,而不是.text
最小工作示例
结果(它将
Hello World
替换B
)编辑:
这个版本从
tds
中获取单个td
(Element.Tag
),将Element.Tag
转换为字符串,替换此字符串中的文本,将新字符串转换回Element.Tag
并替换soup
中的td
,因此它不需要使用完整的HTML作为字符串之前:
之后:
相关问题 更多 >
编程相关推荐