我使用代码通过线程下载图像。
def openUrlHours(driver, urlBase, listTag, listValue, tagA, tagImg):
...
for hrefHour in hrefHours:
openUrlMinutes(driver, urlBase, listTag, listValue, tagA, tagImg)
def openUrlMinutes(driver, urlBase, listTag, listValue, tagA, tagImg):
...
for hrefMinute in hrefMinutes:
openUrlSeconds(driver, listTag, listValue, tagImg)
def openUrlSeconds(driver, listTag, listValue, tagImg):
...
imageSeconds = soup.find("ul", id="grid").find_all("img")
ts = []
for imageSecond in imageSeconds:
...
t = createNewDownloadThread(driver, urlImage, pathImage)
t.daemon = True
t.start()
ts.append(t)
for t in ts:
t.join()
def createNewDownloadThread(driver, urlImage, pathImage):
downloadThread = threading.Thread(target=downloadImage, args=(driver, urlImage, pathImage))
return downloadThread
def downloadImage(driver, urlImage, pathImage):
...
这段代码效果很好,但我想加快速度,所以我尝试了以下代码(只显示修改过)。
def openUrlHours(driver, urlBase, listTag, listValue, tagA, tagImg):
def openUrlMinutes(driver, urlBase, listTag, listValue, tagA, tagImg):
...
ts = []
for hrefMinute in hrefMinutes:
...
t = createOpenUrlSecondsThread(driver, listTag, listValue, tagImg)
t.daemon = True
t.start()
ts.append(t)
for t in ts:
t.join()
def createOpenUrlSecondsThread(driver, listTag, listValue, tagImg):
OpenUrlSecondsThread = threading.Thread(target=openUrlSeconds, args=(driver, listTag, listValue, tagImg))
return OpenUrlSecondsThread
def openUrlSeconds(driver, listTag, listValue, tagImg):
def createNewDownloadThread(driver, urlImage, pathImage):
def downloadImage(driver, urlImage, pathImage):
现在我遇到了错误,我认为使用线程嵌套不是那么容易,接下来我该怎么办?
Exception in thread Thread-24:
self.run()
self._target(*self._args, **self._kwargs)
imageSeconds = imageSeconds = soup.find("ul", id="grid").find_all("img")
return soup.find(listTag, id=listValue).find_all(urlTag)
AttributeError: 'NoneType' object has no attribute 'find_all'