# 案例：pchome24h 商品爬蟲 - 搜索功能

> 撰寫時的直播影片
>
> <https://youtu.be/emMJss6rJGE?t=630>

### 網站的傳輸結構

#### 如何請求

#### Chrome DevTools Network

![](/files/-LpOp_5n4Aecv326YNHY)

### 分析邏輯

* 參考第一個請求

![](/files/-LpOq0_b6efSMBsz2Q6h)

* ```
  原始網址：
  https://ecshweb.pchome.com.tw/search/v3.3/?q=%E8%97%8D%E8%8A%BD%E9%9F%B3%E9%9F%BF
  拆解成
  https://域名/功能/版本/傳輸的數值
  GET是用 【?】 做為傳輸的開頭
  該網址可以看做是
  向 ecshweb.pchome.com.tw/search/v3.3 搜索 %E8%97%8D%E8%8A%BD%E9%9F%B3%E9%9F%BF
  並且
  %E8%97%8D%E8%8A%BD%E9%9F%B3%E9%9F%BF (這段經過了 encodeURI加密) = 藍芽音響
  ```
* 回應的內容

並不包含任何商品訊息，所以第一個請求不是關鍵的請求

![](/files/-LpOr9o586wPftskJWC-)

* 往下找 有回應商品訊息的請求

```
先在 網頁上 找到隨意一個商品的連結
```

![](/files/-LpOrhrj7qMRy9-z9I9O)

```
得到網址為：https://24h.pchome.com.tw/prod/DPAH3C-A9009C1DY
一般我們直接找結尾的幾個像型號一樣的英數(建議不要中文) 以A9009C1DY 為例
然後在 network標簽按ctrl+f搜尋
```

![](/files/-LpOrytcMT5tH4HawHET)

```
會得到已經過濾過 請求內容包含該型號的，網址名稱results應該就是我們要的目標
https://ecshweb.pchome.com.tw/search/v3.3/all/results?q=%E8%97%8D%E8%8A%BD%E9%9F%B3%E9%9F%BF&page=1&sort=sale/dc
```

![](/files/-LpOsBxMBCjDNHIXryMi)

`Result回應的內容為`\
`一串json，因為內容太多貼不上，所以先以圖為準，實際上結構化之後就是如 Preview相同`

![](/files/-LpOs_ifHgne2aL15qOH)

> 結論上來說&#x20;
>
> <https://ecshweb.pchome.com.tw/search/v3.3/all/results>
>
> 是我們的目標網址

## 構建程式碼

目標網址的請求格為下圖

![](/files/-LpOtQszxAsJIrSXHloo)

```
欄位說明
 Genneral = 概要
 Response Headers = 網站回應的Headers頭部
 Request Headers = 我們請求的Headers頭部
 Query String Paramenters = 我們請求的內容

分析結果 
 使用的傳輸方法：GET
 請求的內容有 q , page , sort
 從英文直譯的猜測
 q = 查詢的關鍵字
 page = 第幾頁
 sort = 排序
```

### 程式範例

```
import requests #先引用requests


#目標網址為：https://ecshweb.pchome.com.tw/search/v3.3/all/results?q=關鍵字
#請求範例：https://ecshweb.pchome.com.tw/search/v3.3/all/results?q=%E8%97%8D%E8%8A%BD%E9%9F%B3%E9%9F%BF&page=1&sort=sale/dc

#建構網址
keyword = "藍芽音響"
url = "https://ecshweb.pchome.com.tw/search/v3.3/all/results?q="+keyword+"&page=1&sort=sale/dc"

#嘗試請求
rs = requests.get(url=url)

#輸出結果
data = rs.text
print(data)
```

輸出結果

![](/files/-LpOvQXmwxTLmc1a8a9V)


---

# 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://water-1.gitbook.io/requests/an-li-pchome24h-pa-chong-sou-suo-gong-neng-duo-ye-sou-suo/an-li-pchome24h-shang-pin-pa-chong-sou-suo-gong-neng.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.
