html保存就改动,BeautifulSoup:将更改保存回HTML

此函数利用保存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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值