[問題] Dcard圖片爬蟲遇到https該如何解決

看板Python作者 (半醉夜貓)時間7年前 (2017/05/11 18:47), 編輯推噓1(1015)
留言16則, 5人參與, 最新討論串1/2 (看更多)
如題 下方為程式碼 import requests ,threading from bs4 import BeautifulSoup from urllib.request import urlopen headers ={ 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36' } url = ('http://www.dcard.tw/f/photography/p/226364232.html') res = requests.get(url , headers = headers) soup = BeautifulSoup(res.text, "html.parser") imgs = soup.select('img') for img in imgs: try: fn = img['src'] print(fn) img=urlopen(fn) except Exception as e: print (e) continue with open('./imgs/' + str(fn), 'wb') as f: f.write(img.read()) 上面的url為測試用網址。 我有google爬過文 有看到一種寫法是if re.match(r'^https?://(i.)?(m.)?imgur.com', link['href']): 不過因為Dcard的圖檔是存在src裡 不知該如何修改 第一次發文有錯誤煩請指導 感謝各位大大 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.57.171 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1494499637.A.4E1.html

05/11 22:02, , 1F
你都用 requests 了為什麼還要用 urllib
05/11 22:02, 1F

05/11 22:33, , 2F
??? dcard 是走 https, imgur 也沒有擋 https 爬蟲吧?
05/11 22:33, 2F

05/12 13:03, , 3F
回一樓 因為res變數的那行需要它
05/12 13:03, 3F

05/12 13:04, , 4F
回二樓 會出現http error 403 修改後變成OSError
05/12 13:04, 4F

05/12 13:05, , 5F
Error 22 Invalid argument
05/12 13:05, 5F

05/12 13:07, , 6F
附上目前的程式碼 https://repl.it/HsWh/8
05/12 13:07, 6F

05/12 14:21, , 7F
??? code 跑起來是正常的,無法複製你的 bug, 有 error log?
05/12 14:21, 7F

05/12 21:41, , 8F
Z大 應該這樣講 程式跑起來是正常 不過實際上並沒有
05/12 21:41, 8F

05/12 21:41, , 9F
把圖片下載到電腦(Imgs)中
05/12 21:41, 9F

05/12 22:19, , 10F
....所以你 imgs/ 裡面沒東西?
05/12 22:19, 10F

05/12 23:39, , 11F
是的 請問Z大的可以?
05/12 23:39, 11F

05/20 23:46, , 12F
dcard 有開放api 滿好抓的
05/20 23:46, 12F

06/03 23:23, , 13F
我覺得名稱有斜線在檔案上會出問題
06/03 23:23, 13F

06/03 23:23, , 14F
with open('./imgs/' + str(fn.split("/")[-1])
06/03 23:23, 14F

06/03 23:24, , 15F
(後面自行補齊)開檔存檔留下最後的檔名就好了
06/03 23:24, 15F

06/03 23:28, , 16F
補:https在repl會出問題,不是code的問題
06/03 23:28, 16F
文章代碼(AID): #1P544rJX (Python)
文章代碼(AID): #1P544rJX (Python)