Python 3.x 使用Beauty soup修改href链接内的值
我可以提取href,但我还想修改href链接中的值。 下面是一个例子:-Python 3.x 使用Beauty soup修改href链接内的值,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,我可以提取href,但我还想修改href链接中的值。 下面是一个例子:- Input- <a style="color:#85bc20" href="https://example.com?new=[sample]&hash=[hashkey]"> Unsubscribe
Input-
<a style="color:#85bc20" href="https://example.com?new=[sample]&hash=[hashkey]">
Unsubscribe
</a>
输入-
输出-
<a style="color:#85bc20" href="https://example.com?new="samplevalue"&hash='123'">
Unsubscribe
</a>
from bs4 import BeautifulSoup
html = '<a style="color:#85bc20" href="https://example.com?new=[sample]&hash=[hashkey]"> Unsubscribe </a>'
soup = BeautifulSoup(html, 'html.parser')
for a in soup.find_all('a'):
a['href'] = a['href'].replace("[sample]", "samplevalue").replace('[hashkey]', "123")
<a href="https://example.com?new=samplevalue&hash=123" style="color:#85bc20"> Unsubscribe </a>