我有以下Python代码:
content = webpage.content
soup = Soup(content, 'html.parser')
app_url = scheme + app_identity.get_default_version_hostname() + '/'
for link in soup.find_all(href = True):
if scheme in link['href']:
link['href'] = link['href'].replace(scheme, app_url)
logging.info('@MirrorPage | Updated link: %s', link['href'])
else:
link['href'] = input_url + link['href'].strip('/')
logging.info('@MirrorPage | Updated asset: %s', link['href'])
# https://stackoverflow.com/questions/15455148/find-after-replacewith-doesnt-work-using-beautifulsoup/19612218#19612218
#soup = Soup(soup.renderContents())
# https://stackoverflow.com/questions/14369447/how-to-save-back-changes-made-to-a-html-file-using-beautifulsoup-in-python
content = soup.prettify(soup.original_encoding)
并按如下方式呈现我的HTML:
^{pr2}$其中app_identity
来自googleappengine,jinja2
用于模板化/呈现。我已经尽我所能将修改过的HTML写回content
变量,以便呈现正确的网页。我怎样才能正确地写回我所做的任何更改?我尝试过在适当的地方使用replaceWith
,但这似乎没有达到目的。我做了什么根本性的错误吗?在
更改Google应用程序项目的IMAP首选项下服务帐户的权限修复了写入更改。然而,基本HTML并不能呈现整个页面,也就是说,当呈现像Google这样的站点时,Javascript和样式似乎不起作用。我可以简单地使用自我反应写入(汤),但解决不了这个问题。我将在一个单独的问题中解决这个问题,因为它涉及实际检索(或抓取)指定的网站。在
此函数利用保存html并根据需要返回它以进行重新处理。。在
我在stackoverflow上测试了它,它用替换的links/scheme保存了html。在
我用
{{description}}
作为template.html
中的占位符它将打开的html作为变量返回,然后被传回bs4对象并打印出来。在
相关问题 更多 >
编程相关推荐