BeautifulSoup:将更改保存回HTML

2024-04-19 20:42:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下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,但这似乎没有达到目的。我做了什么根本性的错误吗?在


Tags: ininfoappurllogginghtmllinkcontent
2条回答

更改Google应用程序项目的IMAP首选项下服务帐户的权限修复了写入更改。然而,基本HTML并不能呈现整个页面,也就是说,当呈现像Google这样的站点时,Javascript和样式似乎不起作用。我可以简单地使用自我反应写入(汤),但解决不了这个问题。我将在一个单独的问题中解决这个问题,因为它涉及实际检索(或抓取)指定的网站。在

此函数利用保存html并根据需要返回它以进行重新处理。。在

我在stackoverflow上测试了它,它用替换的links/scheme保存了html。在

我用{{description}}作为template.html中的占位符

它将打开的html作为变量返回,然后被传回bs4对象并打印出来。在

#!/usr/bin/python
# -*- coding: utf-8 -*-

import codecs
from xml.sax.saxutils import escape
import os

import jinja2
import requests
from bs4 import BeautifulSoup as bs4


def revise_links():
    url = 'https://stackoverflow.com/'
    template_name = 'template.html'
    file_name = 'replaced'

    scheme = 'stackoverflow'
    replace_with = 'mysite'

    r = requests.get(url)
    html_bytes = r.text
    soup = bs4(html_bytes, 'lxml')

    description_source = soup.findAll()

    for a in soup.findAll(href=True):
        if scheme in a['href']:
            a['href'] = a['href'].replace(scheme, replace_with)
            print a['href']
        else:
            a['href'] = url + a['href'].strip('/')

    # RENDER THE NEW HTML FILE    *

    def render(tpl_path, context):
        """Render html file with new data. Looks for the file in the current path"""
        (path, filename) = os.path.split(tpl_path)
    return jinja2.Environment(loader=jinja2.FileSystemLoader(path or './')).get_template(filename).render(context)

    # HTML DATA

    context = {'description': description_source}

    # Render the result

    result = render(template_name, context)

    # open the html

    # with open(file_name + '.html', 'a', encoding='utf-8') as f:
    #      f.write(result)  # write result

    # OPEN THE NEW HTML FILE READY TO REVISE **********************

    #  f1 = open(file_name + '.html', 'r', encoding='utf-8')
     # descript = f1.read()

    return result


content = revise_links()
soup = bs4(content, 'lxml')
print soup

相关问题 更多 >