fork download
  1. import urllib
  2. import urllib.request
  3. from bs4 import BeautifulSoup
  4. import os
  5. from string import ascii_lowercase
  6.  
  7. def make_soup(url):
  8. thepage = urllib.request.urlopen(url)
  9. soupdata = BeautifulSoup(thepage, "html.parser")
  10. return soupdata
  11.  
  12. i=1
  13. soup = make_soup("https://2...content-available-to-author-only...h.hk/pr/res/1130518.html")
  14. for img in soup.findAll('img'):
  15. temp = img.get('src')
  16. if temp[:1] == "/":
  17. image = "https://2...content-available-to-author-only...h.hk" + temp
  18. else:
  19. image = temp
  20.  
  21. print(image)
  22. nametemp = img.get('alt')
  23. if len(nametemp) == 0:
  24. filename = str(i)
  25. i = i+1
  26. else:
  27. filename = nametemp
  28.  
  29. imagefile = open(filename + ".png", 'wb')
  30. imagefile.write(urllib.request.urlopen(image).read())
  31. imagefile.close()
Runtime error #stdin #stdout #stderr 0.09s 127040KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "/usr/lib/python3.5/urllib/request.py", line 1254, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/usr/lib/python3.5/http/client.py", line 1107, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.5/http/client.py", line 1152, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.5/http/client.py", line 1103, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/usr/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/usr/lib/python3.5/http/client.py", line 1253, in connect
    super().connect()
  File "/usr/lib/python3.5/http/client.py", line 849, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/lib/python3.5/socket.py", line 694, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.5/socket.py", line 733, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./prog.py", line 13, in <module>
  File "./prog.py", line 8, in make_soup
  File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 466, in open
    response = self._open(req, data)
  File "/usr/lib/python3.5/urllib/request.py", line 484, in _open
    '_open', req)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 1297, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib/python3.5/urllib/request.py", line 1256, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>