【发布时间】:2013-11-14 08:25:19
【问题描述】:
我编写了以下 Python 代码来抓取网站 www.style.com 中的图像
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] 没有这样的文件或目录:'/images/homepage-2013-october/header/logo.png'
如何解决?
这也可以递归查找网站中的所有图像吗?我的意思是主页上没有的其他图片。
谢谢!
【问题讨论】:
-
你提到的错误不在代码中。
-
你应该发布python给出的完整回溯错误
标签: python python-2.7 beautifulsoup web-crawler