3

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/>

4

1 に答える 1

5

2つのことが役立つかもしれません

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" 
>
于 2012-07-31T13:59:38.760 に答える