2017-03-02 56 views
-3

繼「無聊東西自動化」書後,我寫了一個腳本來下載每一個xkcd漫畫。我完全按照本書的代碼(據我所知),但我收到了一個我不明白的錯誤。 Link 回溯:XKCD漫畫拼圖程序錯誤

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/ericdusseau/PycharmProjects/AutomateTheBoringStuff/downloadXkcd.py 
Downloading page http://xkcd,com... 
Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/connection.py", line 141, in _new_conn 
    (self.host, self.port), self.timeout, **extra_kw) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/util/connection.py", line 60, in create_connection 
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM): 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 743, in getaddrinfo 
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags): 
socket.gaierror: [Errno 8] nodename nor servname provided, or not known 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 600, in urlopen 
    chunked=chunked) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 356, in _make_request 
    conn.request(method, url, **httplib_request_kw) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request 
    self._send_request(method, url, body, headers, encode_chunked) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request 
    self.endheaders(body, encode_chunked=encode_chunked) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders 
    self._send_output(message_body, encode_chunked=encode_chunked) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output 
    self.send(msg) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send 
    self.connect() 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/connection.py", line 166, in connect 
    conn = self._new_conn() 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/connection.py", line 150, in _new_conn 
    self, "Failed to establish a new connection: %s" % e) 
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.HTTPConnection object at 0x104f80240>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/adapters.py", line 423, in send 
    timeout=timeout 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 649, in urlopen 
    _stacktrace=sys.exc_info()[2]) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/util/retry.py", line 376, in increment 
    raise MaxRetryError(_pool, url, error or ResponseError(cause)) 
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='xkcd,com', port=80): Max retries exceeded with url:/(Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x104f80240>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',)) 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/Users/ericdusseau/PycharmProjects/AutomateTheBoringStuff/downloadXkcd.py", line 11, in <module> 
    res = requests.get(url) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/api.py", line 70, in get 
    return request('get', url, params=params, **kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/api.py", line 56, in request 
    return session.request(method=method, url=url, **kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 488, in request 
    resp = self.send(prep, **send_kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 609, in send 
    r = adapter.send(request, **kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/adapters.py", line 487, in send 
    raise ConnectionError(e, request=request) 
requests.exceptions.ConnectionError: HTTPConnectionPool(host='xkcd,com', port=80): Max retries exceeded with url:/(Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x104f80240>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',)) 

Process finished with exit code 1 

源碼:

#! python3 
# downloadXKCD.py - Downloads every single XKCD comic. 

import requests, os, bs4 

url = 'http://xkcd,com'    #starting url 
os.makedirs('xkcd', exist_ok=True) # store comics in ./xkcd 
while not url.endswith('#'): 
    #Download the page 
    print('Downloading page %s...' % url) 
    res = requests.get(url) 
    res.raise_for_status() 

    soup = bs4.BeautifulSoup(res.text) 

    #Find the URL of the comic image. 
    comicElem = soup.select('#comic img') 
    if comicElem == []: 
     print('Could not find comic image.') 
    else: 
     try: 
      comicUrl = 'http:' + comicElem[0].get('src') 
      #Download the image. 
      print('Downloading image %s...' % (comicUrl)) 
      res = requests.get(comicUrl) 
      res.raise_for_status() 
     except requests.exceptions.MissingSchema: 
      #Skip this comic 
      prevLink = soup.select('a[rel="prev"]')[0] 
      url = 'http://xkcd.com' + prevLink.get('href') 
      continue 


    #Save the image to ./xkcd 
    imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb') 
    for chunk in res.iter_content(100000): 
     imageFile.write(chunk) 
    imageFile.close() 


    #Get the Prev button's url. 
    prevLink = soup.select('a[rel="prev"]')[0] 
    url = 'htt[://xkcd.com' + prevLink.get('href') 

print('Done.') 
+3

'URL =「HTTP:// XKCD,com''這應該是'.com',不' com' – Andy

+0

我可以看到幾個重新定義的基地網址。而且,在你的代碼的底部,你的網址看起來不正常:'htt [://'。 – Aif

回答

4

你輸出給你一個想法問題所在:

Downloading page http://xkcd,com... 
Traceback (most recent call last): 

http://xkcd,com是一個無效的URL。它應該是.com,而不是,com


說到不正確的網址,你也有另一種無效的一個後面的代碼:

url = 'htt[://xkcd.com' + prevLink.get('href') 
     ^Should be 'p' 
0

有在xkcd,com一個逗號。不是一個點......

0

存在具有下載隨機XKCD漫畫形象,並設置圖像作爲腳本一個Git倉庫一幅壁紙。您可以設置在cron爲每分鐘,這樣它會改變桌面壁紙:

xkcd_random_wallpaper