【发布时间】:2021-06-07 00:00:08
【问题描述】:
我正在尝试编写一个 python 脚本,其中将自动修改多个 html 文件中的某些标签;从终端运行单个命令。
我构建了代码库。
在我的代码库中,我做了如下所示的事情。有没有更方便的方法可以用更少的代码做到这一点?
#modifying the 'src' of <img> tag in the soup obj
for img in soup.findAll('img'):
img['src'] = '{% static ' + "'" + img['src'] + "'" + ' %}'
#modifying the 'href' of <link> tag in the soup obj
for link in soup.findAll('link'):
link['href'] = '{% static ' + "'" + link['href'] + "'" + ' %}'
#modifying the 'src' of <script> tag in the soup obj
for script in soup.findAll('script'):
script['src'] = '{% static ' + "'" + script['src'] + "'" + ' %}'
例如,我可以在单个 for 循环中而不是 3 中执行吗?并不是说它必须像我在下面写的那样,任何好的实践建议都是我正在寻找的。p>
for img, link, script in soup.findAll('img', 'link', 'script'):
rest of the code goes here....
【问题讨论】:
标签: python html performance beautifulsoup automation