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