Python を学習し、Web ユーティリティも作成しようとしています。私が達成しようとしているタスクの 1 つは、ローカルで実行できる単一の html ファイルを作成することですが、元の Web ページのように見えるために必要なすべてにリンクしています。(なぜこれが必要なのかを尋ねる場合、それは私が作成しているユーティリティの一部として機能する可能性があるため、またはそうでない場合は単に教育のためである可能性があるためです) 理論的な質問と実際的な質問の 2 つがあります。
1) これは、(機能的ではなく) 視覚的な目的で可能ですか? オンラインで必要なものすべてにリンクしながら、HTML ページをオフラインで使用できますか? または、HTMLファイル自体をWebサーバーで実行することについての基本的なことで、これが可能にならない場合は? 私はそれでどこまで行くことができますか?
2) HTML ページ上のリンクされた要素を非相対化する (それを作成する) python スクリプトを開始しましたが、私は初心者なので、外部リソースにもリンクするいくつかの要素または属性を見逃している可能性があります。いくつかのページを試してみたところ、以下のコードのページが正しく機能しないことに気付きました。正しくリンクされていない .js ファイルのようです。(これから起こる多くの問題の最初のもの) 私の最初の質問に対する答えが、少なくとも部分的に「はい」だったと仮定すると、この Web サイトのコードを修正するのを手伝ってくれる人はいますか?
ありがとうございました。
更新、これのスクリプトタグを見逃しましたが、追加した後でも正しく動作しません。
import lxml
import sys
from lxml import etree
from StringIO import StringIO
from lxml.html import fromstring, tostring
import urllib2
from urlparse import urljoin
site = "www.script-tutorials.com/advance-php-login-system-tutorial/"
output_filename = "output.html"
def download(site):
response = urllib2.urlopen("http://"+site)
html_input = response.read()
return html_input
def derealitivise(site, html_input):
active_html = lxml.html.fromstring(html_input)
for element in tags_to_derealitivise:
for tag in active_html.xpath(str(element+"[@"+"src"+"]")):
tag.attrib["src"] = urljoin("http://"+site, tag.attrib.get("src"))
for tag in active_html.xpath(str(element+"[@"+"href"+"]")):
tag.attrib["href"] = urljoin("http://"+site, tag.attrib.get("href"))
return lxml.html.tostring(active_html)
active_html = ""
tags_to_derealitivise = ("//img", "//a", "//link", "//embed", "//audio", "//video", "//script")
print "downloading..."
active_html = download(site)
active_html = derealitivise(site, active_html)
print "writing file..."
output_file = open (output_filename, "w")
output_file.write(active_html)
output_file.close()
さらに、すべての要素をチェックすることで、コードをより詳細にすることができました...
このように見えますが、すべての要素を反復処理する正確な方法はわかりません。これは別の問題であり、おそらく誰かが応答するまでにそれを理解するでしょう...:
def derealitivise(site, html_input):
active_html = lxml.html.fromstring(html_input)
for element in active_html.xpath:
for tag in active_html.xpath(str(element+"[@"+"src"+"]")):
tag.attrib["src"] = urljoin("http://"+site, tag.attrib.get("src"))
for tag in active_html.xpath(str(element+"[@"+"href"+"]")):
tag.attrib["href"] = urljoin("http://"+site, tag.attrib.get("href"))
return lxml.html.tostring(active_html)
アップデート
Burhan Khalid のソリューションのおかげで、一見実行するには単純すぎるように見えましたが、機能するようになりました。コードは非常に単純なので、ほとんどの人はそれを必要としないでしょうが、役立つ場合はとにかく投稿します:
import lxml
import sys
from lxml import etree
from StringIO import StringIO
from lxml.html import fromstring, tostring
import urllib2
from urlparse import urljoin
site = "www.script-tutorials.com/advance-php-login-system-tutorial/"
output_filename = "output.html"
def download(site):
response = urllib2.urlopen(site)
html_input = response.read()
return html_input
def derealitivise(site, html_input):
active_html = html_input.replace('<head>', '<head> <base href='+site+'>')
return active_html
active_html = ""
print "downloading..."
active_html = download(site)
active_html = derealitivise(site, active_html)
print "writing file..."
output_file = open (output_filename, "w")
output_file.write(active_html)
output_file.close()
これらすべてと、非常に単純であるにもかかわらず、スクリプトにリストした Web サイトで実行されている .js オブジェクトは、まだ正しく読み込まれません。これを修正できるかどうかは誰にもわかりませんか?