2017-04-26 6 views
1

일부 제품에 대한 정보를 추출하는 웹 사이트를 고칠 수 있지만 가격에 문제가 있습니다. 내 코드는 다음R에서 다운로드 한 소스 코드와 웹 사이트의 소스 코드의 차이점

> enlace<-"http://www.carulla.com/products/0000687608965009/Crema+Dental+Sensitive+Proalivio+Colgate" 
> download.file(enlace, destfile = "scrapedpage.html", quiet=TRUE) 
> doc<-read_html("scrapedpage.html") 
> # description 
> toString(xml_find_all(doc,xpath=paste0('//*[@id="pdpProduct"]/div[3]/h3'))) 
[1] "<h3 class=\"pdpInfoProductName\" itemprop=\"name\">Crema Dental Sensitive Proalivio Colgate</h3>" 
> # reference 
> toString(xml_find_all(doc,xpath=paste0('//*[@id="pdpProduct"]/div[3]/p'))) 
[1] "<p class=\"pdpInfoProductRef\">\r\n\t\t\t\t\t\t\t\t\tPresentación:C \r\n\t\t\t\t\t\t\t\t\tPLU:739983</p>" 
> # prices 
> toString(xml_find_all(doc,xpath=paste0('//*[@id="pdpProduct"]/div[3]/div[1]/div[2]/h4'))) 
[1] "" 

는 나는이를 찾아 원래의 페이지에서 소스 코드에서이 정보를 확인 :

<div class="pdpInfoProduct pull-left"> 
      <h3 class="pdpInfoProductName" itemprop="name">Crema Dental Sensitive Proalivio Colgate</h3> 
      <h2 class="pdpInfoProductBrand" itemprop="brand">COLGATE</h2> 
      <p class="pdpInfoProductRef"> 
           Presentación:C&nbsp; 
           PLU:739983</p> 
         <div class="pdpInfoProductPrices"> 
       <div class="pull-right"> 
          <div class="pro-big-Ovalo"> 
           <p>25%</p> 
          </div> 
         </div> 
        <div class="pdpInfoProductPrice" itemprop="offers" itemscope itemtype="http://schema.org/Offer"> 

       <meta itemprop="priceCurrency" content="COP" /> 
        <meta itemprop="price" content="17213.0" /> 
        <h4 class="priceOffer"> 
         $17.213</h4> 
        <h6 class="before">Antes: <span class="strikeText"> 
           $22.950</span> 
         </h6> 
        </div> 
      </div> 

관심의 내 정보는 $ 17.213되지만 내가 다운로드 할 때 R과 소스 코드, 나는 다음과 같은 구하십시오입니다

> con2<-url(enlace,"r") 
> x<-readLines(con2) 
> close(con2) 
> x[1270:1285] 
[1] "\t\t\t\t\t\t\t\t\tPLU:739983</p>"                                     
[2] "\t\t\t\t\t\t\t<div class=\"pdpInfoProductPrices\">\t"                               
[3] "\t\t\t\t\t<div class=\"pdpInfoProductPrice\" itemprop=\"offers\" itemscope itemtype=\"http://schema.org/Offer\">"                
[4] "\t\t\t\t\t"                                         
[5] "\t\t\t\t\t<meta itemprop=\"priceCurrency\" content=\"COP\" />"                            
[6] "      <meta itemprop=\"price\" content=\"\" />"                          
[7] "\t\t\t\t\t\t<h4 class=\"price\">"                                    
[8] "\t\t\t\t\t\t\t</h4>"                                       
[9] "\t\t\t\t\t\t</div>"                                       
[10] "\t\t\t\t</div>"                                        
[11] "\t\t\t\t"                                         
[12] "\t\t\t\t\t\t\t\t\t"                                        
[13] "\t\t\t\t\t\t\t\t\t\t\t\t\t <div class=\"product-seller row-fluid\">"                             
[14] "\t\t\t\t  <!-- +++++ Carulla Seller +++++ -->            "                   
[15] "        <p> Vendido por: &nbsp Carulla</p>                          " 
[16] "     </div>" 

, 내가 얻을 \ t \ t \ t \ t \ t \ t \ t 대신 $ 17.213.

귀하의 도움에 매우 감사드립니다.

답변

0

웹 사이트에서 UA와 쿠키를 확인하여 수행중인 작업을 정확하게 수행하지 못하게하려고합니다. 방금 wget을 사용하여 다운로드를 시도했지만 평면적으로 403 금지됨 오류가 발생했습니다.

웹 스크래핑의 아이디어는 현재 상업용 페이지의 경우 요즘에는 쓸모가 없습니다. 몇 가지 해결 방법이 있습니다 (예를 들어, download.file()에 대한 도움말을 확인하고 wget 및 curl의 맨 페이지를 읽고 UA 및 가져 오기 쿠키를 변경하는 방법을 확인하십시오). 그러나 실제로이 작업을 대규모로 수행하려는 경우 브라우저 스크립팅을 조사한 다음 해당 데이터를 R로 가져올 수 있습니다.

사이트 소유자가 원하지 않는 일을하고 있다는 것을 명심하십시오. 즉, R과 거의 관계가 없습니다.