这个问题需要一些时间来介绍,请耐心等待。如果你能到达那里,解决它会很有趣。这个scrape将使用循环在本网站的数千页上复制。
我正在试图抓住网站http://www.digikey.com/product-detail/en/207314-1/A25077-ND/,希望用Digi-Key零件编号,可用数量等来捕获表格中的数据。包括价格突破,单价,延长价格的右侧。
使用R函数readHTMLTable()不起作用,只返回NULL值。这个(我相信)的原因是因为网站使用html代码中的“aspNetHidden”标签隐藏了它的内容。
由于这个原因,我也发现使用htmlTreeParse()和xmlTreeParse()时出现困难,整个部分没有出现在结果中。
中的R函数scrape()require(scrapeR)
URL<-scrape("http://www.digikey.com/product-detail/en/207314-1/A25077-ND/")
会返回完整的html代码,包括感兴趣的行:
<th align="right">Digi-Key Part Number</th>
<td id="reportpartnumber">
<meta itemprop="productID" content="sku:A25077-ND">A25077-ND</td>
<th>Price Break</th>
<th>Unit Price</th>
<th>Extended Price
</th>
</tr>
<tr>
<td align="center">1</td>
<td align="right">2.75000</td>
<td align="right">2.75</td>
但是,我无法从这段代码中选择节点并返回错误:
no applicable method for 'xpathApply' applied to an object of class "list"
我使用不同的功能收到了错误,例如:
xpathSApply(URL,'//*[@id="pricing"]/tbody/tr[2]')
getNodeSet(URL,"//html[@class='rd-product-details-page']")
我不是最熟悉的xpath,但是已经使用网页上的inspect元素识别xpath并复制xpath。
非常感谢你能给予的任何帮助!
答案 0 :(得分:2)
你没有看过刮刮的帮助吗?它返回一个列表,你需要获取该列表的一部分(如果parse = TRUE),依此类推。
另外我认为网页正在做一些沉重的浏览器检测。如果我从命令行尝试wget
页面,我会得到一个错误页面,scrape
函数可以获得一些可用的东西(但似乎与你不同),Chrome会获得包含所有编码内容的完整垃圾。呸。这对我有用:
> URL<-scrape("http://www.digikey.com/product-detail/en/207314-1/A25077-ND/")
> tables = xpathSApply(URL[[1]],'//table')
> tables[[2]]
<table class="product-details" border="1" cellspacing="1" cellpadding="2">
<tr class="product-details-top"/>
<tr class="product-details-bottom">
<td class="pricing-description" colspan="3" align="right">All prices are in US dollars.</td>
</tr>
<tr>
<th align="right">Digi-Key Part Number</th>
<td id="reportpartnumber"><meta itemprop="productID" content="sku:A25077-ND"/>A25077-ND</td>
<td class="catalog-pricing" rowspan="6" align="center" valign="top">
<table id="pricing" frame="void" rules="all" border="1" cellspacing="0" cellpadding="1">
<tr>
<th>Price Break</th>
<th>Unit Price</th>
<th>Extended Price
</th>
</tr>
<tr>
<td align="center">1</td>
<td align="right">2.75000</td>
<td align="right">2.75</td>
调整你的用例,在这里我得到所有的表并显示第二个表,其中包含你想要的信息,其中一些表可以在pricing
表中直接获得:< / p>
pricing = xpathSApply(URL[[1]],'//table[@id="pricing"]')[[1]]
> pricing
<table id="pricing" frame="void" rules="all" border="1" cellspacing="0" cellpadding="1">
<tr>
<th>Price Break</th>
<th>Unit Price</th>
<th>Extended Price
</th>
</tr>
<tr>
<td align="center">1</td>
<td align="right">2.75000</td>
<td align="right">2.75</td>
</tr>
等等。