Spaces:
Sleeping
Sleeping
Download fetch_pets.py from fromnowoncoding/pet_scrapper: direct link, hf CLI and curl.
- Browser
- Download file 5.07 kB
-
https://huggingface.co/spaces/fromnowoncoding/pet_scrapper/resolve/main/fetch_pets.py
- Command line
-
hf download hf://spaces/fromnowoncoding/pet_scrapper/fetch_pets.py
-
curl -L -o fetch_pets.py https://huggingface.co/spaces/fromnowoncoding/pet_scrapper/resolve/main/fetch_pets.py
5.07 kB
| 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") | |