【问题标题】:Make a list of Items from steam Marketplace列出 Steam 市场中的项目
【发布时间】:2019-01-04 21:39:29
【问题描述】:

我想用 Steam 市场中的某些物品填充数据库表,特别是目前来自 CSGO 的枪支。我似乎无法找到所有枪支名称、皮肤名称和皮肤质量的任何数据库或列表,这正是我想要的。

我想到的一种方法是找到我想要的项目列表,例如“霰弹枪”,并将页面上的每个项目保存到数据库中,然后遍历该搜索的每个页面。例如: http://steamcommunity.com/market/search?appid=730&q=shotgun#p1_default_desc http://steamcommunity.com/market/search?appid=730&q=shotgun#p2_default_desc 等等..

首先,我不确定该怎么做,其次,我想知道是否有更简单的方法。

我计划稍后通过将名称替换为以下内容来使用商品名称来获取价格:http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29

通过对每件商品运行检查,每隔一小时左右更新一次价格。 (可能至少几千..)

【问题讨论】:

    标签: php mysql database


    【解决方案1】:

    您需要做的事情的一般要点归结为:

    • 确定需要解析的网址。在您的情况下,您会注意到结果是通过 ajax 加载的。右键单击该页面,单击“检查元素”并转到网络选项卡。你会看到实际的 url 是:http://steamcommunity.com/market/search/render/?query=&start=<STARTVALUE>&count=<NUMBEROFRESULTS>&search_descriptions=0&sort_column=quantity&sort_dir=desc&appid=730&category_730_ItemSet%5B%5D=any&category_730_TournamentTeam%5B%5D=any&category_730_Weapon%5B%5D=any&category_730_Type%5B%5D=tag_CSGO_Type_Pistol&category_730_Type%5B%5D=tag_CSGO_Type_SMG&category_730_Type%5B%5D=tag_CSGO_Type_Rifle&category_730_Type%5B%5D=tag_CSGO_Type_SniperRifle&category_730_Type%5B%5D=tag_CSGO_Type_Shotgun&category_730_Type%5B%5D=tag_CSGO_Type_Machinegun&category_730_Type%5B%5D=tag_CSGO_Type_Knife
    • 确定响应类型是什么。在这种情况下它是 json,我们想要的数据在 html-sn-p 中
    • 找到解析它所需的框架。您可以使用json_decode(...) 来解码 json 字符串。 This question 将提供更多关于如何解析 html 的信息。
    • 您现在可以将这些 url 提供给加载页面的函数。您可以使用file_get_contents(...)curl library
    • 将从响应中解析的值输入到数据库中。确保脚本在运行时间过长时不会被杀死。 This question 将为您提供更多相关信息。

    您可以将以下内容用作框架。您必须自己弄清楚 html 的结构,并查找要使用的 html 解析器和 mysql 库的教程。

    <?php
      //Prevent this script from being killed. Please note that if this script never
      //ends, you'll have to kill it manually
      set_time_limit( 0 );
    
      //The api does not allow for more than 100 results at a time
      $start = 0;
      $count = 100;
      $maxresults = PHP_INT_MAX;
      $baseurl = "http://steamcommunity.com/market/search/render/?query=&start=$1&count=$2&search_descriptions=0&sort_column=quantity&sort_dir=desc&appid=730&category_730_ItemSet%5B%5D=any&category_730_TournamentTeam%5B%5D=any&category_730_Weapon%5B%5D=any&category_730_Type%5B%5D=tag_CSGO_Type_Pistol&category_730_Type%5B%5D=tag_CSGO_Type_SMG&category_730_Type%5B%5D=tag_CSGO_Type_Rifle&category_730_Type%5B%5D=tag_CSGO_Type_SniperRifle&category_730_Type%5B%5D=tag_CSGO_Type_Shotgun&category_730_Type%5B%5D=tag_CSGO_Type_Machinegun&category_730_Type%5B%5D=tag_CSGO_Type_Knife";
    
      while( $start < $maxresults ) {
        //Constructing the next url
        $url = str_replace( "$1", $start, $baseurl );
        $url = str_replace( "$2", $count, $url );
    
        //Doing the request
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        $result = json_decode( curl_exec( $ch ), TRUE );
        curl_close( $ch );
    
        //Doing things with the result
        //
        //First let's see if everything went according to plan
        if( $result == NULL || $result["success"] !== TRUE ) {
          echo "Something went horribly wrong. Please edit the script to take this error into account and rerun it.";
          exit( -1 );
        }
    
        //Bookkeeping for the next url we have to fetch
        $count = $result["pagesize"];
        $start += $count;
        $maxresults = $result["total_count"];
    
        //This is the html we have to parse
        $html = $result["results_html"];
    
        //Look up an example how to parse html, and how to get data from it
        //Look up how to make a database connection and how to insert data into
        //your database
      }
    
      echo "And we are done!";
    

    【讨论】:

    • 感谢您的回答。当我检查元素 > 网络时,没有任何内容可以显示该 URL,但是,当我访问该 URL 时显示的信息看起来可能是我需要的。但我仍然不确定如何访问它。我可以看到你已经列出了我应该做的事情,但我没想到还有这么多东西要学。尽管我很想学习所有额外的信息来做这件事,但这个项目是在时间线上的,我没有时间。我还有其他可能的方法吗,或者你能帮我写一些代码让我开始吗?
    • @Samurai8 我知道我不应该这样做,但非常感谢您提供代码!它有很大帮助!但是有一个问题,您注意到如果脚本永远不会结束,我将不得不手动终止它。有理由不结束吗?当我在测试代码时它工作得很好,我看不到我应该在哪里添加一个参数来看看它是否没有结束。
    • 写得不好的函数,或者你调用的函数可能会卡住并且永远不会完成。我没有大量阅读 curl_* 函数,所以我不确定这是否真的是这些函数的问题。我知道套接字,在某些情况下,函数可能不知道套接字已关闭,并继续等待该套接字上发生的事情。你可能会想,当一个套接字关闭时,该套接字上永远不会发送任何数据,因此在该套接字上读取数据的函数将不会做任何事情,直到有其他东西唤醒它。
    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 2015-08-18
    • 2016-02-21
    • 2016-03-12
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 2021-08-30
    相关资源
    最近更新 更多