[Python crawler] Urlretrieve error using urllib function [socket Error][errno 10054]

Source: Internet
Author: User
Tags socket error

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.