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

Source: Internet
Author: User
Tags socket error

In order to practicing, using reptiles to climb a "you know" figure bed, using the Urlretrieve function, not only slow, but also always error, not open timeout is the above mentioned socket error.

There are many ways to find online, such as adding headers to a request, and calling Urllib2. Methods such as close () need to be invoked after Request.urlopen (). Read () did not work.
Because do not want to trouble scrapy and so on library, so found a simple and rude way:
Open the data stream directly with the open function with Urllib and save it in binary write file:
Reference Code Snippet: The method in which the annotation is originally replaced

# Urlretrieve Speed Slow unstable
# urllib.urlretrieve (i, Path + '%s.jpg '% imgnum)
Urlopen = Urllib. Urlopener ()
#下载图片流
fp = Urlopen.open (IMAGEURL)
data = Fp.read ()
#清除并以二进制写入
f = open (path + ' 1.jpg ') , ' w+b ')
f.write (data)
F.close ()
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.