In order to practiced hand, use the crawler to climb a "you know" bed, using the Urlretrieve function, not only slow, but also always error, not open timeout is the above mentioned socket error.
Find many ways on the internet such as adding headers in the request, calling Urllib2. Request.urlopen (). The call to close () after read () has not worked.
Because do not want to trouble scrapy wait for the library, so found a simple rough way:
Open the data stream directly using the Urllib's open function, and then save it as a binary write file:
Reference snippet: Where the comment is the original method that was replaced
# urlretrieve速度慢不稳定# urllib.urlretrieve(i, path +‘%s.jpg‘ % ImgNum)urlopen = urllib.URLopener()#下载图片流fp = urlopen.open(imageUrl)data = fp.read()#清除并以二进制写入open‘1.jpg‘‘w+b‘)f.write(data)f.close()
[python crawler] Urlretrieve error using urllib function [socket Error][errno 10054]