我写了下面的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 –