【问题标题】:Getting price from a retailer website [closed]从零售商网站获取价格 [关闭]
【发布时间】:2012-05-01 07:24:44
【问题描述】:

我正在构建一个 iOS 和 Android 应用程序,它可以扫描条形码并显示来自零售商网站的该书的产品页面。但现在,我只想从该产品页面获取价格,而不是整个页面。

如何从页面中提取产品的价格,就像 RedLaser 使用它自己的应用程序一样。

产品页面:http://goo.gl/rDxAg 价格:321 卢比

我想要this 之类的东西,它可以在 iOS 和 Android 上实现,无需使用外部服务器。

我是新手,任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试过联系零售商询问是否有可用的 API?
  • 快速搜索显示 Flipkart 没有可用于访问定价等产品信息的(公共)API。你总是可以求助于数据抓取,也就是说你会拉出产品的页面,并找到相关信息,但这是他们自己的terms of use不允许的。当然,如果您想违反这些条款,那完全取决于您。

标签: android ios web barcode


【解决方案1】:

如果网站的官方 API 不可用,那么您必须解析下载的 html 以获取您想要的数据。有许多适用于 iOS 和 Android 的第三方 html 解析器库。

对于 iOS,请查看parsing HTML on the iPhone

对于 Android,请查看Parse HTML in Android

两个链接中都有一些代码示例向您展示如何做到这一点。

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    在此简要介绍之后提供了一个jsFiddle Demo

    您正在使用的当前产品页面包含太多数据,只是为了获取价格。

    最好使用 Flipkart.com 移动图书网站,因为这样加载速度更快。

    参考1:http://www.flipkart.com/m/books

    由于您的应用程序必须已经在使用图书的 pid 号,您可以查询移动网页搜索!您问题中的链接适用于 pid9780224060875 的书

    参考2:http://www.flipkart.com/m/search-all?query=9780224060875

    在该页面上,您可以看到图书价格在 Span Tag 内,其中 Class Namesp

    <!-- Fragment of product price format -->
    <div id="productpage-price">
     <p>
         Price:  <del> Rs. 350</del>
      <span class="sp">Rs. 263</span>
     </p>
    </div>
    

    然后,使用 jQuery,你可以像这样得到你需要的价格数据:

    // Begin section to show random methods to use HTML values
    
        // Get the HTML of  "Rs. 263" and store it in variable as a string.
        var priceTextAndLabel = $('#productpage-price').find('span.sp').text();
    
        // Get the HTML of  "Rs. 263" and slice off the first 4 characters of "Rs. " leaving "263" only.
        // Adjust the .slice() if possiable that number is after decimal point. Example: "Rs.1000"
        var priceText = $('#productpage-price').find('span.sp').text().slice(4);
    
        // As above but convert text string of "263" to a number (to allow JavaScript Math if req.).
        // The value 10 seen below reflects decimal base 10 (vs, octal(8) example) for .parseInt();
        var priceNumber = parseInt($('#productpage-price').find('span.sp').text().slice(4),10);
    
        // Firefox with Firebug Console will show BLACK characters for "Rs. 263" since it's a "string".
        console.log( priceTextAndLabel );
    
        // Firefox with Firebug Console will show BLACK characters for "263" since it's a "string".
        console.log( priceText );
    
        // Firefox with Firebug Console will show BLUE characters for "263" since it's a "number".
        console.log( priceNumber );
    
    // End section to show random method to use HTML values
    

    好的,现在是关键部分...您一直在等待的部分...这就是如何在您的目标(甚至网页)中使用 Flipkart.com 搜索 URL。

    可悲的答案是你不能。他们不仅禁止它,还阻止它。这意味着您不能 iframe 网页,甚至不能使用 AJAX 加载搜索 URL。

    为了说明上述失败,这里有一个 jsFiddle Demo,当使用浏览器控制台查看时,将显示 AJAX 连接完成后没有获得任何内容。

    参考3:jsFiddle flipkart.com Demo


    推荐的解决方案:这里只有一个真正的选择。使用具有可用 API 的书店。该 API 可能具有用于特权访问的 API 密钥,可让您成为合法的商店代表。

    也许他们最终会提供 API。现在,他们有一个Mobile App Store 用于收藏 MP3。看到 MP3 如何反映有声读物,他们也为图书提供移动应用商店可能只是时间问题。

    【讨论】:

      【解决方案3】:

      获取产品页面的url,提取价格可以使用Nokogiri

      您首先需要获取页面内容,然后使用某种方法获取价格。你可以通过 CSS 或 xpath 来做到这一点

      来自 Nokogiri 的基本示例:

      require 'nokogiri'
      require 'open-uri'
      
      doc = Nokogiri::HTML(open('http://www.YOUR_URL_HERE.com'))
      price = doc.at_xpath("//span[@id='fk-mprod-our-id']").text
      

      【讨论】:

      • 你能帮我举个例子吗
      • 好吧,我写的例子确实可以完成这项工作,但它是用 ruby​​ 编写的,我看到你打算在 iOS 应用程序中使用这些数据。为什么要添加 ruby​​ 标签?您是否在 ruby​​ 中有一些服务器端代码为您的 iOS 应用程序提供响应?
      • 不,对不起那个 Ruby 标签。请你告诉我如何在 iOS 或 CSS 中实现
      • 我不确定你知道 CSS 是什么...
      【解决方案4】:

      如果零售商提供,您可以使用 API。搜索它!
      如果没有可用的 API,您可以从零售商服务器请求页面并将 HTML 解析为 XML 以获取包含价格的元素。但是,如果零售商更改其网站,这可能会被打破。另外,问问他是否允许你使用他的价格。

      【讨论】:

        【解决方案5】:

        我在电子商务中工作,有时对于某些 CSV,我需要从供应商网站获取数据,您可以编写一个例程,在这种情况下,某些网站使用一个元素,您可以在此处找到价格:

        xpath: //div[3]/div[2]/div/div/div/span
        

        就像这个使用 Selenium 和 Perl 的例子:

        open (INFO, '>>file.csv') or die "$!";  
        my $sel = Test::WWW::Selenium->new( host => "localhost", 
                                            port => 4444, 
                                            browser => "*chrome", 
                                            browser_url => "http://www.example.com/page.htm" );
        $sel->open_ok("/page.htm");
        $sel->click_ok("//table[2]/tbody/tr/td/a/img");
        $sel->wait_for_page_to_load_ok("30000");
        my $price = $sel->get_text("//div[3]/div[2]/div/div/div/span");
        print INFO ("$price\n");
        $sel->go_back_ok();
        
        # Close file
        close (INFO);
        

        您可以使用类似的功能来抓取数据,或使用其他解决方案进行网页抓取

        【讨论】:

        • 你能帮我举个例子吗,我如何在 CSS 中使用上面的内容
        • 谢谢,但我想实现这是一个iOS App,所以我不知道该怎么做
        【解决方案6】:
        <span class="price final-price our fksk-our" id="fk-mprod-our-id">
           Rs.
           <span class="small-font"> </span>
           315
        </span>
        

        我注意到这个HTML 是为你的Price tag 准备的。

        我会建议你使用jSoupDownload from here

        现在使用这个库,解析变得更容易了,你所要做的就是。

         Document doc = null;
        
            try{
                doc = Jsoup.connect("You page URL comes here").get(); // get url contents
            }catch(IOException e){
                 // Handle exception here.
            }
        
         String priceHtml = doc.select("#fk-mprod-our-id").get(0).html().trim(); // get specific tag
         System.out.println("html fetched: "+priceHtml); //print to check if right tag is selected
         priceHtml = priceHtml.replace("((<span(.)*?>)(.)*(</span>))", ""); // replace inner span using regex.
         System.out.println("My Price tag: "+priceHtml); 
        

        我没有测试过上面的代码,但它必须工作。它可能包含小错误。但只要稍加努力,您就可以让它发挥作用。

        Parsing 数据有时需要时间。您必须在后台执行此操作。在后台解析完成后,将数据发布到您的 UI 线程。

        编辑:

        try catch包围你的connect电话。

        并确保您在androidManifest.xml 中设置了以下权限

        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.INTERNET" />
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-17
        • 1970-01-01
        • 2020-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多