# 发送您的第一个请求

使用Thordata的 Web Scraper API发送您的第一个请求之前，您需要一个API Token。

* 您可以在\[ API 构建器 ]页面获得免费试用。
* 然后，复制“Token”部分中的凭证。

{% hint style="info" %}
*备注：您获得的 Token 属于敏感信息，请妥善保管，切勿泄露。如因 Token 泄露导致您的资源被滥用，相关后果需由您自行承担，本平台不对此负责。*
{% endhint %}

代码示例：\
获取 API 凭证后，使用以下代码发送您的第一个请求：

{% tabs %}
{% tab title="cURL-Linux" %}

```sh
curl -X POST "https://scraperapi.thordata.com/builder" \
  -H "Authorization: Bearer Token-ID" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "spider_name=amazon.com" \
  -d "spider_id=amazon_product_by-url" \
  -d "spider_parameters=[{\"url\": \"https://www.amazon.com/HISDERN-Checkered-Handkerchief-Classic-Necktie/dp/B0BRXPR726\",\"zip_code\": \"94107\"}]" \
  -d "spider_errors=true" \
  -d "file_name={{TasksID}}"
```

{% endtab %}

{% tab title="cURL-Windows" %}

```sh
curl -X POST "https://scraperapi.thordata.com/builder" ^
  -H "Authorization: Bearer Token-ID" ^
  -H "Content-Type: application/x-www-form-urlencoded" ^
  -d "spider_name=amazon.com" ^
  -d "spider_id=amazon_product_by-url" ^
  -d "spider_parameters=[{\"url\": \"https://www.amazon.com/HISDERN-Checkered-Handkerchief-Classic-Necktie/dp/B0BRXPR726\",\"zip_code\": \"94107\"}]" ^
  -d "spider_errors=true" ^
  -d "file_name={{TasksID}}"
```

{% endtab %}

{% tab title="Python" %}

```python
import requests
import json

def main():
  client = requests.Session()
  target_url = "https://scraperapi.thordata.com/builder"

  spider_parameters = [
    {
      "url": "https://www.amazon.com/HISDERN-Checkered-Handkerchief-Classic-Necktie/dp/B0BRXPR726",
      "zip_code": "94107"
    }
  ]

  spider_parameters_json = json.dumps(spider_parameters)
  
  form_data = {
    "spider_name": "amazon.com",
    "spider_id": "amazon_product_by-url",
    "spider_parameters": spider_parameters_json,
    "spider_errors": "true",
    "file_name": "{{TasksID}}"
  }

  headers = {
    "Authorization": "Bearer Token-ID",
    "Content-Type": "application/x-www-form-urlencoded"
  }

  try:
    resp = client.post(target_url, data=form_data, headers=headers)
    resp.raise_for_status()  # Raises an HTTPError for bad responses
    
    print(f"Status Code: {resp.status_code}")
    print(f"Response Body: {resp.text}")
      
  except requests.exceptions.RequestException as e:
    print(f"Error sending request: {e}")

if __name__ == "__main__":
  main()
```

{% endtab %}

{% tab title="Go" %}

```go
package main

import (
  "encoding/json"
  "fmt"
  "io"
  "net/http"
  "net/url"
  "strings"
)

func main() {
  client := &http.Client{}
  targetURL := "https://scraperapi.thordata.com/builder"

  spiderInfo := []map[string]string{ 
    {
      "url": "https://www.amazon.com/HISDERN-Checkered-Handkerchief-Classic-Necktie/dp/B0BRXPR726",
      "zip_code": "94107",
    },
  }

  spiderInfoJSON, err := json.Marshal(spiderInfo)
  if err != nil {
    fmt.Println("Error marshaling JSON:", err)
    return
  }
  
  formData := url.Values{}
  formData.Add("spider_name", "amazon.com")
  formData.Add("spider_id", "amazon_product_by-url")
  formData.Add("spider_parameters", string(spiderInfoJSON))
  formData.Add("spider_errors", "true")
  formData.Add("file_name", "{{TasksID}}")
  
  req, err := http.NewRequest("POST", targetURL, strings.NewReader(formData.Encode()))
  if err != nil {
    fmt.Println("Error creating request:", err)
    return
  }

  req.Header.Set("Authorization", "Bearer Token-ID")
  req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

  resp, err := client.Do(req)
  if err != nil {
    fmt.Println("Error sending request:", err)
    return
  }
  defer resp.Body.Close()

  body, err := io.ReadAll(resp.Body)
  if err != nil {
    fmt.Println("Error reading response:", err)
    return
  }

  fmt.Printf("Status Code: %d\n", resp.StatusCode)
  fmt.Printf("Response Body: %s\n", body)
}
```

{% endtab %}

{% tab title="C#" %}

```csharp
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;

class Program
{
  static async Task Main(string[] args)
  {
    var client = new HttpClient();
    var targetUrl = "https://scraperapi.thordata.com/builder";

    var spiderInfo = new List<Dictionary<string, string>>
    {
      new Dictionary<string, string>
      {
        {"url", "https://www.amazon.com/HISDERN-Checkered-Handkerchief-Classic-Necktie/dp/B0BRXPR726"},
        {"zip_code", "94107"}
      }
    };
    
    var spiderInfoJson = Newtonsoft.Json.JsonConvert.SerializeObject(spiderInfo);
    
    var formData = new Dictionary<string, string>
    {
      { "spider_name", "amazon.com" },
      { "spider_id", "amazon_product_by-url" },
      { "spider_parameters", spiderInfoJson },
      { "spider_errors", "true" },
      { "file_name", "{{TasksID}}"}
    };

    var request = new HttpRequestMessage(HttpMethod.Post, targetUrl)
    {
      Content = new FormUrlEncodedContent(formData)
    };

    request.Headers.Add("Authorization", "Bearer Token-ID");

    try
    {
      var response = await client.SendAsync(request);
      var responseBody = await response.Content.ReadAsStringAsync();

      Console.WriteLine($"Status Code: {(int)response.StatusCode}");
      Console.WriteLine($"Response Body: {responseBody}");
    }
    catch (HttpRequestException e)
    {
      Console.WriteLine($"Error sending request: {e.Message}");
    }
  }
}
```

{% endtab %}

{% tab title="Node" %}

```n4js
const axios = require('axios');
const qs = require('querystring');

async function main() {
  const targetURL = "https://scraperapi.thordata.com/builder";

  const spiderInfo = [
    {
      url: "https://www.amazon.com/HISDERN-Checkered-Handkerchief-Classic-Necktie/dp/B0BRXPR726",
      zip_code: "94107"
    }
  ];
  
  try {
    const spiderInfoJSON = JSON.stringify(spiderInfo);
    
    const formData = qs.stringify({
      spider_name: "amazon.com",
      spider_id: "amazon_product_by-url",
      spider_parameters: spiderInfoJSON,
      spider_errors: true,
      file_name: "{{TasksID}}"
    });

    const response = await axios.post(targetURL, formData, {
      headers: {
        'Authorization': 'Bearer Token-ID',
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    });

    console.log(`Status Code: ${response.status}`);
    console.log(`Response Body: ${JSON.stringify(response.data)}`);
  } catch (error) {
    if (error.response) {
      console.error(`Error response: ${error.response.status} - ${error.response.data}`);
    } else if (error.request) {
      console.error('No response received:', error.request);
    } else {
      console.error('Error:', error.message);
    }
  }
}

main();


```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

$targetURL = "https://scraperapi.thordata.com/builder";

$spiderInfo = [
  [
    "url" => "https://www.amazon.com/HISDERN-Checkered-Handkerchief-Classic-Necktie/dp/B0BRXPR726",
    "zip_code" => "94107"
  ]
];

$spiderInfoJSON = json_encode($spiderInfo);
if ($spiderInfoJSON === false) {
  die("Error marshaling JSON: " . json_last_error_msg());
}

$formData = [
  "spider_name" => "amazon.com",
  "spider_id" => "amazon_product_by-url",
  "spider_parameters" => $spiderInfoJSON,
  "spider_errors" => true,
  "file_name" => "{{TasksID}}"
];

$options = [
  "http" => [
    "method" => "POST",
    "header" => implode("\r\n", [
      "Authorization: Bearer Token-ID",
      "Content-Type: application/x-www-form-urlencoded",
      "Accept: application/json"
    ]),
    "content" => http_build_query($formData)
  ]
];

$context = stream_context_create($options);
$response = file_get_contents($targetURL, false, $context);

if ($response === false) {
  $error = error_get_last();
  die("Error sending request: " . $error["message"]);
}

// Get HTTP status code
$statusCode = null;
if (isset($http_response_header[0])) {
  preg_match('/HTTP\/\d\.\d (\d{3})/', $http_response_header[0], $matches);
  $statusCode = $matches[1] ?? null;
}

echo "Status Code: " . $statusCode . "\n";
echo "Response Body: " . $response . "\n";
?>
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
require 'json'
require 'net/http'
require 'uri'

begin
  target_url = URI.parse('https://scraperapi.thordata.com/builder')

  spider_parameters = [
    {
      url: 'https://www.amazon.com/HISDERN-Checkered-Handkerchief-Classic-Necktie/dp/B0BRXPR726',
      zip_code: '94107'
    }
  ]

  spider_parameters_json = spider_parameters.to_json
  
  form_data = URI.encode_www_form(
    spider_name: 'amazon.com',
    spider_id: 'amazon_product_by-url',
    spider_parameters: spider_parameters_json,
    spider_errors: true,
    file_name: '{{TasksID}}'
  )

  http = Net::HTTP.new(target_url.host, target_url.port)
  path = target_url.query ? "#{target_url.path}?#{target_url.query}" : target_url.path
  request = Net::HTTP::Post.new(path)
  request['Authorization'] = 'Bearer Token-ID'
  request['Content-Type'] = 'application/x-www-form-urlencoded'
  request.body = form_data

  response = http.request(request)

  puts "Status Code: #{response.code}"
  puts "Response Body: #{response.body}"

rescue JSON::GeneratorError => e
  puts "Error generating JSON: #{e.message}"
rescue URI::InvalidURIError => e
  puts "Invalid URL: #{e.message}"
rescue Net::HTTPError => e
  puts "HTTP Error: #{e.message}"
rescue StandardError => e
  puts "Error: #{e.message}"
end
```

{% endtab %}

{% tab title="Java" %}

```java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    String targetURL = "https://scraperapi.thordata.com/builder";

    // Create the spider info data structure
    List<Map<String, String>> spiderInfo = new ArrayList<>();
    
    Map<String, String> param1 = new HashMap<>();
      param1.put("url", "https://www.amazon.com/HISDERN-Checkered-Handkerchief-Classic-Necktie/dp/B0BRXPR726");
      param1.put("zip_code", "94107");
      spiderInfo.add(param1);

    
    try {
      // Convert spiderInfo to JSON
      String spiderInfoJSON = new com.google.gson.Gson().toJson(spiderInfo);
      
      // Prepare form data
      String formData = "spider_name=" + URLEncoder.encode("amazon.com", StandardCharsets.UTF_8) + 
      "&spider_id=" + URLEncoder.encode("amazon_product_by-url", StandardCharsets.UTF_8) + 
      "&spider_parameters=" + URLEncoder.encode(spiderInfoJSON, StandardCharsets.UTF_8) + 
      "&spider_errors=" + URLEncoder.encode("true", StandardCharsets.UTF_8) + 
      "&file_name=" + URLEncoder.encode("{{TasksID}}", StandardCharsets.UTF_8);

      // Create and configure the connection
      HttpURLConnection connection = (HttpURLConnection) new URL(targetURL).openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Authorization", "Bearer Token-ID");
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      connection.setDoOutput(true);

      // Send the request
      try (OutputStream os = connection.getOutputStream()) {
        byte[] input = formData.getBytes(StandardCharsets.UTF_8);
        os.write(input, 0, input.length);
      }

      // Get the response
      int statusCode = connection.getResponseCode();
      String responseBody;
      try (InputStream is = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
        StringBuilder response = new StringBuilder();
        String responseLine;
        while ((responseLine = br.readLine()) != null) {
          response.append(responseLine.trim());
        }
        responseBody = response.toString();
      }

      System.out.println("Status Code: " + statusCode);
      System.out.println("Response Body: " + responseBody);

    } catch (Exception e) {
      System.err.println("Error: " + e.getMessage());
      e.printStackTrace();
    }
  }
}
```

{% endtab %}
{% endtabs %}

发送请求后，结果将以JSON/CSV/xlsx格式返回，您可以在“[任务](https://dashboard.thordata.com/zh/web-scraper/tasks)”列表中查看或下载这些结果。

<figure><img src="/files/cos3riemsheL51K8ZKcG" alt=""><figcaption></figcaption></figure>

如果您需要更多帮助，请通过邮箱<support@thordata.com>联系我们。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.thordata.com/doc/zh/web-scraper-api/fa-song-nin-de-di-yi-ge-qing-qiu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
