1
#!usr/bin/env python
# coding:utf-8

from socket import * 
import urllib2
import urllib
import re

def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html

def getImg(html):
    rule = r'src="(.*?\.jpg)" width'
    imgre = re.compile(rule)
    imglist = re.findall(imgre,html)
    i = 0
    for imgurl in imglist:
        i += 1
        urllib.urlretrieve(imgurl,'%d.jpg' %i)

html = getHtml("https://tieba.baidu.com/index.html")

print "downloading....................."
getImg(html)enter code here

When I run the code,the html and imglist could be got,the follow errors are also showed,now I don't know how to do it.

Traceback (most recent call last): File "C:/Users/18817/Desktop/getjpg.py", line 27, in getImg(html) File "C:/Users/18817/Desktop/getjpg.py", line 21, in getImg urllib.urlretrieve(imgurl,'E:/getjpg/%s.jpg' %i) File "D:\Python27\lib\urllib.py", line 98, in urlretrieve return opener.retrieve(url, filename, reporthook, data) File "D:\Python27\lib\urllib.py", line 245, in retrieve fp = self.open(url, data) File "D:\Python27\lib\urllib.py", line 213, in open return getattr(self, name)(url) File "D:\Python27\lib\urllib.py", line 457, in open_https return self.http_error(url, fp, errcode, errmsg, headers) File "D:\Python27\lib\urllib.py", line 377, in http_error result = method(url, fp, errcode, errmsg, headers) File "D:\Python27\lib\urllib.py", line 642, in http_error_302 headers, data) File "D:\Python27\lib\urllib.py", line 669, in redirect_internal return self.open(newurl) File "D:\Python27\lib\urllib.py", line 213, in open return getattr(self, name)(url) File "D:\Python27\lib\urllib.py", line 350, in open_http h.endheaders(data) File "D:\Python27\lib\httplib.py", line 1038, in endheaders self._send_output(message_body) File "D:\Python27\lib\httplib.py", line 882, in _send_output self.send(msg) File "D:\Python27\lib\httplib.py", line 844, in send self.connect() File "D:\Python27\lib\httplib.py", line 821, in connect self.timeout, self.source_address) File "D:\Python27\lib\socket.py", line 575, in create_connection raise err IOError: [Errno socket error] [Errno 10060]

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • Check this stack overflow post https://stackoverflow.com/questions/15820739/python-urlerror-urlopen-error-errno-10060 – Max Aug 23 '17 at 05:14
  • urllib.urlopen(....) works fine! when execute urllib.urlretrieve(), can't download the picture from the internet – weimeitiankong980 Aug 23 '17 at 05:19
  • I installed Python2.7 and Anaconda, are the conflict?@DYZ – weimeitiankong980 Aug 23 '17 at 05:24
  • Thank you for your help! I have found the causes, which are the inaccurately or uncompleted regular expression(re),so there are wrong url in the list for urllib.urlretrieve().I revised it and got aright results. – weimeitiankong980 Aug 23 '17 at 12:40

0 Answers0