我寫了下面的Python代碼從網站www.style.comPython的BeautifulSoup網頁圖像抓取器IO錯誤:[錯誤2]沒有這樣的文件或目錄
import urllib2, urllib, random, threading
from bs4 import BeautifulSoup
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class Images(threading.Thread):
def __init__(self, lock, src):
threading.Thread.__init__(self)
self.src = src
self.lock = lock
def run(self):
self.lock.acquire()
urllib.urlretrieve(self.src,'./img/'+str(random.choice(range(9999))))
print self.src+'get'
self.lock.release()
def imgGreb():
lock = threading.Lock()
site_url = "http://www.style.com"
html = urllib2.urlopen(site_url).read()
soup = BeautifulSoup(html)
img=soup.findAll(['img'])
for i in img:
print i.get('src')
Images(lock, i.get('src')).start()
if __name__ == '__main__':
imgGreb()
抓取圖像,但我得到這個錯誤:
IOError: [Errno 2] No such file or directory: '/images/homepage-2013-october/header/logo.png'
如何解決?
也可以遞歸地找到網站中的所有圖像?我的意思是其他圖像不在主頁上。
謝謝!
你提到的錯誤是無處代碼。 – aIKid
你應該發佈由python –