Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用Beauty soup修改href链接内的值_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 3.x 使用Beauty soup修改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

我可以提取href,但我还想修改href链接中的值。 下面是一个例子:-

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>