[問題] 某電商購物網站爬蟲請益

看板Python作者 (rodyforeter)時間6年前 (2019/01/03 05:16), 6年前編輯推噓5(507)
留言12則, 5人參與, 6年前最新討論串1/1
各位大大安 最近在練習爬電商網站的商品資訊 目標網站是yahoo, pchome, momo 現在我已經可以爬yahoo跟pchome 最近在爬momo 發現momo好像有做反爬 試了好幾天 一直爬不下來 說明我試過的方法 ____________________________________________ 從network那裡試著找有沒有api可以發request → 發現是用 post 的方式,帶params跟form_data params有兩個 n 跟 t n = 2002、2018 等等,先假設這個值固定 t 是utc+8 int 時間戳記 (ex:1546461729893) 之後encode utf-8 再貼在 url + "?" + 後面 form_data 直接照著發 然後 header的部分也有加 user-agent 試過用 requests.post 跟urllib.request.urlopen(request, data=form_data) 可是抓回來的都是一堆空白跟換行 沒辦法抓到json資料 不知是我有遺漏的地方 還是momo自己有做一些反爬機制讓bot抓不到資料 ____________________ 於是嘗試第二種方式 webDriver 因為我是在雲端linux ubuntu 16.04上面跑爬蟲 所以環境設定的部分其實不太會用 盡力使用了selenium + firefox + geckodriver 卻發現當我把目標網址丟進某個list裡面foreach去跑driver.get(url) 迴圈跑個兩三次就停住了 不斷修改測試後仍然失敗 現在連要發request去抓準備丟進list裡面的url都直接Message: connection refused 程式碼大概像這樣:: options = Options() options.headless = True options.set_capability("marionette", False) driver = webdriver.Firefox(options=options, executable_path='/home/ubuntu/geckodriver') driver.set_page_load_timeout(20) driver.set_script_timeout(20) i = 1 for url in web_drive_urls: str = "https://www.momoshop.com.tw" + url print(str) driver.get(str) #time.sleep(5) print(i) i+=1 不知是否有好方法能成功爬取momo的商品資料? 理想上我希望可以用第一種方式直接得到json 不然就只好暫時放棄爬momo 謝謝大大們指點 補上我想抓的商品頁範例: https://www.momoshop.com.tw/category/DgrpCategory.jsp?d_code=1106700047 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 27.246.168.39 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1546463785.A.6C8.html ※ 編輯: rodyforeter (27.246.168.39), 01/03/2019 05:44:52

01/03 07:30, 6年前 , 1F
你是不是接了 某個 Case XD
01/03 07:30, 1F

01/03 08:18, 6年前 , 2F
WOW
01/03 08:18, 2F

01/03 12:42, 6年前 , 3F
我這一看就超新手啊 1F太看得起我了XD
01/03 12:42, 3F

01/03 14:25, 6年前 , 4F
你要做什麼功能要不要說說看 我試試我的
01/03 14:25, 4F

01/03 16:16, 6年前 , 5F
用fiddler看看他傳出去的封包跟你發送的有沒有差別
01/03 16:16, 5F

01/04 10:28, 6年前 , 6F
抓一些商品資訊
01/04 10:28, 6F

01/04 10:29, 6年前 , 7F
fiddler抓https好像要另外設定 但我後來發現在robots
01/04 10:29, 7F

01/04 10:30, 6年前 , 8F
.txt 裡面disallow對內部api的爬取
01/04 10:30, 8F

01/04 10:31, 6年前 , 9F
所以現在應該會改為用web driver 雖然很慢又不穩..
01/04 10:31, 9F

01/04 10:43, 6年前 , 10F
謝謝提供使用fiddler的方式抓封包
01/04 10:43, 10F
https://imgur.com/a/24OpIDl 遇到這個問題 selenium.common.exceptions.WebDriverException: Message: Failed to decode response from marionette Code https://imgur.com/a/yyRXPg4 ※ 編輯: rodyforeter (36.229.82.253), 01/04/2019 11:22:38

01/04 17:39, 6年前 , 11F
不用github表達?
01/04 17:39, 11F

01/04 17:54, 6年前 , 12F
可以用 GitHub Gist
01/04 17:54, 12F
文章代碼(AID): #1SBIefR8 (Python)
文章代碼(AID): #1SBIefR8 (Python)