如何使用Go修改HTML文件中的元素?

huangapple go评论104阅读模式
英文:

How do I modify elements of an HTML file with Go?

问题

我已经尝试了几天,甚至用谷歌搜索最有效的方法,但还是无法解决。我一直在尝试的是使用Go语言的库(http://golang.org/x/net/html)打开一个HTML文件,并修改其中的img标签和它们的源代码,使其指向一个已知的目录和文件集合。到目前为止,我已经能够使用以下代码找到这些元素:

// 打开文件并返回一个名为file的变量
file, _ = os.Open(file.Name())
// 创建文档
doc, err := html.Parse(file)
// 检查生成文档时是否有错误
check(err)
// 使用匿名函数查找带有img标签的标签
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "img" {
for _, img := range n.Attr {
if img.Key == "src" {
str := breakdownURL(img.Val) // 获取 ../../resource/(thing.h23.jpg) <-- 这部分
// 创建一个静态地址,添加到动态地址上
address := filepath.Join(filepath.Join("Resources", getFileNotExt(file)), str)
img.Val = address
break
}
}
}

for at := n.FirstChild; at != nil; at = at.NextSibling {
    f(at)
}

}

f(doc)

这段代码能够找到元素并追加正确的目录,但它只修改了这个文档文件。我不知道如何将其追加到实际文件中。我唯一的想法是将文档以某种写入方式打开,并将文档中的新数据复制到文件中。非常感谢您的帮助!谢谢您花费时间!

英文:

I've been trying to figure this out for a few days and I've kind of exhausted even the most effective of google searching. What I've been trying to do is to open a file of the type HTML and with Go's library (http://golang.org/x/net/html) modify the img tags and their source to a known directory and set of files. So far I've been able to find the elements using this,

//Open the file and return a variable called file.
file, _ = os.Open(file.Name())
//Create the doc
doc, err := html.Parse(file)
//Check for err when generating the doc
check(err)
//Look for tags with img using an anonymous function.
var f func(*html.Node)
f = func(n *html.Node) {
	if n.Type == html.ElementNode &amp;&amp; n.Data == &quot;img&quot; {
		for _, img := range n.Attr {
			if img.Key == &quot;src&quot; {
				str := breakdownURL(img.Val) //Gets the ../../resource/(thing.h23.jpg) &lt;-- That
				//Creating a static address to add to the dynamic one
				address := filepath.Join(filepath.Join(&quot;Resources&quot;, getFileNotExt(file)), str)
				img.Val = address
				break
			}
		}
	}

	for at := n.FirstChild; at != nil; at = at.NextSibling {
		f(at)
	}
}

f(doc)

That's been able to find the elements and append the correct directory but it's only modifying this doc file. I have no clue how to append it to the actual file. The only thought that I have is opening the doc as some kind of writing way and copying the new data from the doc to the file. Any help is greatly appreciated! Thank you so much for taking your time :).

答案1

得分: 2

你应该确保保存编辑后的文档。

首先,打开文件以进行读写和截断操作:

file, err := os.OpenFile("sample.html", os.O_RDWR | os.O_TRUNC, 0644)

在处理完成后,覆盖原始文件:

html.Render(file, doc)
英文:

You should definitely save the edited document.

First, open the file for read/write and truncate:

file, err := os.OpenFile(&quot;sample.html&quot;, os.O_RDWR | os.O_TRUNC, 0644)

And after you finish processing, override the original file:

html.Render(file, doc)

huangapple
  • 本文由 发表于 2017年2月19日 14:00:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/42324166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定