Spaces:
Sleeping
Sleeping
File size: 5,065 Bytes
7f57139 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | import requests
import pandas as pd
import json
import time
import base64
def fetch_pets(shelter_code=None, animal_type="0", page_size=20, max_pages=10):
"""抓取收容所的宠物資料
Args:
shelter_code (str, optional): 收容所代碼,例如:'AAAFG'
animal_type (str): 動物類型,'0'=狗,'1'=貓,''=全部
page_size (int): 每頁顯示數量
max_pages (int): 最大頁數
"""
api_url = "https://www.pet.gov.tw/handler/AnimalsCore.ashx"
base_url = "https://www.pet.gov.tw"
headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "https://www.pet.gov.tw",
"Referer": "https://www.pet.gov.tw/AnimalApp/AnnounceMent_Announce.aspx?PageType=null",
"User-Agent": "Mozilla/5.0",
"X-Requested-With": "XMLHttpRequest"
}
all_pets = []
for page in range(1, max_pages + 1):
print(f"\n正在抓取第 {page} 頁")
# 構建請求參數
front_param = {
"action": "AnnounceMentData_Announce",
"_FrontParam": {
"PageSize": page_size,
"PageNo": page,
"AcNum": "",
"CNum": "",
"Shelter": shelter_code if shelter_code else "",
"aType": animal_type,
"aBreed": "",
"PageType": "Announce",
"PetSex": "",
"HAIRCOLOR": "",
"County": "",
"Age": "",
"County2": "",
"DistrictTeam": ""
}
}
payload = f"Method=AnimalsFront&Param={json.dumps(front_param)}"
try:
response = requests.post(api_url, headers=headers, data=payload, verify=False)
print(f"狀態碼: {response.status_code}")
result = response.json()
print(f"回應內容: {result}")
if not result.get('Success'):
print("⚠️ 無更多資料,停止抓取")
break
# 解析 JSON 資料
pets_data = json.loads(result.get('Message', '[]'))
if not pets_data:
print("⚠️ 找不到寵物資料,停止抓取")
break
for pet in pets_data:
try:
# 構建圖片網址
pic = pet.get('pic', '')
img_url = f"{base_url}/Upload/pic/{pic}.png" if pic else ''
# 構建詳細頁面網址
accept_num = pet.get('AcceptNum', '')
user_tag = pet.get('UserTag', '')
if accept_num and user_tag:
accept_num_b64 = base64.b64encode(accept_num.encode()).decode()
user_tag_b64 = base64.b64encode(user_tag.encode()).decode()
full_url = f"{base_url}/AnimalApp/AnnounceSingle.aspx?PageType=Adopt&AcNum={accept_num_b64}&UT={user_tag_b64}"
pet_info = {
'收容編號': pet.get('AcceptNum', ''),
'品種': pet.get('breedName', '').strip(),
'類別': pet.get('typeName', '').strip(),
'性別': pet.get('sexName', ''),
'來源': pet.get('areaName', ''),
'收容所': pet.get('ShelterName', ''),
'詳細頁面': full_url,
'圖片網址': img_url
}
all_pets.append(pet_info)
except Exception as e:
print(f"解析宠物資料時發生錯誤: {e}")
continue
print(f"✅ 已解析 {len(pets_data)} 筆資料")
# 避免請求太頻繁
time.sleep(1)
except Exception as e:
print(f"❌ 抓取資料時發生錯誤: {e}")
break
# 將結果儲存成 CSV
df = pd.DataFrame(all_pets)
# 根據篩選條件決定檔名
filename_parts = []
if shelter_code:
filename_parts.append(f"shelter_{shelter_code}")
if animal_type:
filename_parts.append("dogs" if animal_type == "0" else "cats")
output_filename = "_".join(filename_parts) if filename_parts else "all_pets"
output_filename = f"{output_filename}.csv"
df.to_csv(output_filename, index=False, encoding="utf-8-sig")
print(f"\n✅ 已儲存 {len(df)} 筆資料到 {output_filename}")
return df
if __name__ == "__main__":
# 範例:抓取特定收容所的狗狗資料
fetch_pets(shelter_code="AAAFG", animal_type="0")
# 範例:抓取所有收容所的資料
# fetch_pets()
# 範例:只抓取貓咪資料
# fetch_pets(animal_type="1")
|