gpt4 book ai didi

xml - 防止 `xmlValue` 剥离
标签

转载 作者:数据小太阳 更新时间:2023-10-29 02:24:26 26 4
gpt4 key购买 nike

我有一个问题,其中 xmlValue剥离 <br />我需要保留的标签(或转换为我可以 strsplit 打开的其他字符。

这是一个例子:

> f <- htmlParse(getForm("http://sites.target.com/site/en/spot/store_locator_popups.jsp", ajax="true", storeNumber=1889), asText=TRUE)
> xpathSApply(f, "//div[@class=\"sl_results_popup_address\"]", xmlValue)
[1] "1154 S Clark StChicago, IL 60605(312) 212-6300"

与它正在解析的 HTML 相比:

<div class="sl_results_popup_address">
1154 S Clark St
<br/>
Chicago, IL 60605
<br/>
(312) 212-6300
</div>

我试过了 , recursive=FALSE但这似乎没有帮助。

如果它们是 <p></p>换行然后它会更容易,因为我可以单独捕获它们,但使用 <br/>不包装文字我真的不能朝那个方向走。希望有一个选项可以减少在 xmlValue 内完成的剥离级别(或者 <br/> 可能在文档解析阶段被剥离?)。

最佳答案

有两件事可能会有所帮助

app.data<-getForm("http://sites.target.com/site/en/spot/store_locator_popups.jsp", ajax="true", storeNumber=1889)
app.data<-gsub("<br>","\n",app.data)
f <- htmlParse(app.data, asText=TRUE)
out<-xpathSApply(f, "//div[@class=\"sl_results_popup_address\"]", xmlValue)
> xpathSApply(f, "//div[@class=\"sl_results_popup_address\"]", xmlValue)
[1] "1154 S Clark St\nChicago, IL 60605\n(312) 212-6300"
>

所以只需将 br 标签替换为其他内容或使用您的原始代码即可

> xpathSApply(f, "//div[@class=\"sl_results_popup_address\"]/text()", xmlValue)
[1] "1154 S Clark St" "Chicago, IL 60605" "(312) 212-6300"
>

如果你想保留标签

dum.fun<-function(x){if(xmlName(x)=="br"){"<br/>"}else{xmlValue(x)}}
xChild<-xpathSApply(f, "//div[@class=\"sl_results_popup_address\"]",xmlChildren)
lapply(xChild,dum.fun)
> unlist(lapply(xChild,dum.fun))
[1] "1154 S Clark St" "<br/>" "Chicago, IL 60605"
[4] "<br/>" "(312) 212-6300"
>

关于xml - 防止 `xmlValue` 剥离 <br/> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11741318/

26 4 0