Hello 各位大神
我的系統是Windows 10 x64
我用Python跑以下的程式想練習抓網頁圖片
但是彈出的結果都是
"重複"
"重複"
"重複".......
請問我哪裡有錯或是哪個路徑少了呢?
import os
import urllib.request
def imgs(url):
    try:
        res = urllib.request.urlopen(rep)
        rep = urllib.request.Request(url)
        html = res.read().decode("utf-8", 'ignore')
        # print(html)
        import re
        from bs4 import BeautifulSoup
        web = BeautifulSoup(html, features="html.parser")
        img = web.select("img[src*=/uploads/allimg/]")
        for s in img:
            img_url = "http://pic.netbi.com" + s.get("src")
            file_path = 'D:/DOWNLOAD'
            file_name = "img" + str(int(random.uniform(20, 10) * 10 ** 14))
            if not os.path.exists(file_path):
                # 建立路徑
                os.makedirs(file_path)
                # 獲得圖片字尾
            file_suffix = os.path.splitext(img_url)[1]
            print(file_suffix)
            # 拼接圖片名(包含路徑)
            filename = '{}{}{}{}'.format(file_path, os.sep, file_name, file_suffix)
            print(filename)
            # 下載圖片,並儲存到資料夾中
            urllib.request.urlretrieve(img_url, filename=filename)
    except:
        print("重複")
i = 1
try:
    while i < 1092:
        url = "http://pic.netbi.com/index"
        if i == 1:
            url += ".html"
        else:
            url += "_" + str(i) + ".html"
        imgs(url)
        i += 1
except:
    print("錯誤")
懇請大神指教
為啥你們都喜歡自定義錯誤訊息 ? 然後不會 debug
# 你的問題
img = web.select("img[src*=/uploads/allimg/]")
img = web.select("img[src*='/uploads/allimg/']")
不會 debug 就老老實實秀 error message 出來
try:
    #do somthing...
except Exception as e:
    print(e)
基本上就算你抓下來的圖都一樣你也不會跑去 error 那邊 那你設 except 幹嘛

你都唸完了,那我就不唸了。
其實我是看人家網頁在一行一行拆著學
所以程度非常低 還請見諒~
那你要慎選教材了
剛剛嘗試了一下
謝謝你~ 成功了目前
想請問你們 '/uploads/allimg/' 是因為什麼原因需要加上''呢?
因為
select("img[src*='/uploads/allimg/']") 
你要中間這段
img[src*='/uploads/allimg/']
找 img src 屬性為 /uploads/allimg/ 的 element
你可以看一下 html img 裡 src 屬性也是有用 '' 括起來
我看上面的code一直在想為什麼except那邊代表重複
我另外問一下為啥要把import放在函數裡面??
是我孤陋寡聞???
請問 listennn08 大大,您有推薦的Python抓圖教材嗎?
ositonegro
抓圖應該可以看這系列的前面幾篇
https://ithelp.ithome.com.tw/articles/10202121
除非是需要 selenium 的網頁,那你要另外去找資料