[問題] 爬蟲資料格式及處理

看板Python作者 (阿文)時間8年前 (2017/10/25 22:12), 8年前編輯推噓4(407)
留言11則, 6人參與, 8年前最新討論串1/1
大家好,初學爬蟲 透過以下程式抓到文字資料 https://imgur.com/mGAOY9I
https://imgur.com/GwWX8f3
想問一下這是哪種資料類型阿? 有像html之於BeautifulSoup的library可以處理嗎? 感恩! import requests user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5)' headers = {'User-Agent': user_agent, 'Referer':'http://www.family.com.tw/marketing/inquiry.aspx'} response = requests.get("http://api.map.com.tw/net/familyS" "hop.aspx?searchType=ShopList&type=" "&city=%E5%9F%BA%E9%9A%86%E5%B8%82" "&area=%E4%BB%81%E6%84%9B%E5%8D%80&" "road=&fun=showStoreList&key=6F30E8" "BF706D653965BDE302661D1241F8BE9EBC" , headers=headers) response.text -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.136.49.4 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1508940751.A.DCD.html

10/25 22:23, 8年前 , 1F
看起來是 JSON,Python 有內建函數庫。
10/25 22:23, 1F

10/25 22:23, 8年前 , 2F
你把前面的 showStoreList( 去掉,還有最後的 ) 去掉,
10/25 22:23, 2F

10/25 22:25, 8年前 , 3F
就可以用 json.loads 讀取 JSON
10/25 22:25, 3F

10/25 22:27, 8年前 , 4F
JSONP 用來繞過same origin policy的
10/25 22:27, 4F

10/26 00:39, 8年前 , 5F
你網址裡面有一串key參數 這應該是csrf token吧
10/26 00:39, 5F

10/26 00:40, 8年前 , 6F
get的時候要記得更新這個key
10/26 00:40, 6F

10/26 00:41, 8年前 , 7F
還有個fun參數 應該是用來決定回傳的資料格式
10/26 00:41, 7F

10/26 00:41, 8年前 , 8F
你可以研究一下怎麼讓他直接傳回json
10/26 00:41, 8F

10/26 08:41, 8年前 , 9F
eval(response.text.strip()) probably get a dict
10/26 08:41, 9F

10/26 08:49, 8年前 , 10F
樓上說的沒錯 要把showStoreList去掉才eval得出東西
10/26 08:49, 10F

10/26 15:13, 8年前 , 11F
JSON
10/26 15:13, 11F
把showStoreList去掉就可順利讀取,感恩! fun的部分有storeTownList和showStoreList 一個列出有店點的行政區;一個是店點資料 key的部分目前看似乎都長一樣 https://imgur.com/DVCqwNu
https://imgur.com/3Tar5R8
※ 編輯: unhumanWu (220.136.49.4), 10/26/2017 20:43:07
文章代碼(AID): #1Py9lFtD (Python)