php - 列出 steam 市场中的元素列表

标签 php mysql database

我想用 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

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

最佳答案

您需要做的事情的总体要点可以归结为:

  • 标识您需要解析的网址。在您的情况下,您会注意到结果是通过 ajax 加载的。右键单击该页面,单击“检查元素”并转到网络选项卡。您会看到实际的网址是: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-snippet 内
  • 找到解析它所需的框架。您可以使用json_decode(...)解码 json 字符串。 This question将提供更多如何解析 html 的信息。
  • 您现在可以将这些网址提供给加载页面的函数。您可以使用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!";

关于php - 列出 steam 市场中的元素列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29715253/

相关文章:

PHP 类构造函数将 mysql 行作为对象返回

php - PHP脚本中的Mysql

php - 检索自增数据库值

php - 选择最近 7 天添加的数据

SQL选择存在子集的所有行

php - 想要从多个类别中获取mysql中按类别分组的产品

javascript - Style.Top 属性不会持久

php - MySQL 更新 PHP 和 JS

javascript - PHP 脚本无法收集调用 html 页面的文件名

java - 如何使用 Java 将数据从 4 个文本字段发送到 Netbeans 中的 Derby 数据库