2016-11-05 13 views
1

私は本書「Pythonを使った退屈なタスクを自動化する」に続き、複数のコミックをhttp://xkcd.com から同時にダウンロードするprogrmaを作成しようとしていますが、いくつかの問題がありました。私は本と同じプログラムをコピーしています。ここでPythonのマルチスレッド

は私のコードです:

# multidownloadXkcd.py - Downloads XKCD comics using multiple threads. 

import requests, os ,bs4, threading 

os.chdir('c:\\users\\patty\\desktop') 
os.makedirs('xkcd', exist_ok=True) # store comics on ./xkcd 

def downloadXkcd(startComic, endComic):    
    for urlNumber in range(startComic, endComic):     
     #Download the page 
     print('Downloading page http://xkcd.com/%s...' %(urlNumber)) 
     res = requests.get('http://xkcd.com/%s' % (urlNumber)) 
     res.raise_for_status() 

     soup= bs4.BeautifulSoup(res.text, "html.parser")   

     #Find the URL of the comic image. 
     comicElem = soup.select('#comic img') 
     if comicElem == []: 
      print('Could not find comic image.') 
     else: 
      comicUrl = comicElem[0].get('src') 
      #Download the image. 
      print('Downloading image %s...' % (comicUrl)) 
      res = requests.get(comicUrl, "html.parser") 
      res.raise_for_status() 

      #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() 

downloadThreads = []    # a list of all the Thread objects 
for i in range(0,1400, 100):  # loops 14 times, creates 14 threads 
    downloadThread = threading.Thread(target=downloadXkcd, args=(i, i + 99)) 
    downloadThreads.append(downloadThread) 
    downloadThread.start() 

# Wait for all threads to end. 
for downloadThread in downloadThreads: 
    downloadThread.join() 
print('Done.') 

私は次の例外取得しています:

Exception in thread Thread-1: 
Traceback (most recent call last): 
    File "C:\Python\Python35\lib\threading.py", line 914, in _bootstrap_inner 
    self.run() 
    File "C:\Python\Python35\lib\threading.py", line 862, in run 
    self._target(*self._args, **self._kwargs) 
    File "C:\Users\PATTY\PycharmProjects\CH15_TASKS\practice.py", line 13, in downloadXkcd 
    res.raise_for_status() 
    File "C:\Python\Python35\lib\site-packages\requests\models.py", line 862, in raise_for_status 
    raise HTTPError(http_error_msg, response=self) 
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: http://xkcd.com/0 
Exception in thread Thread-2: 
Traceback (most recent call last): 
    File "C:\Python\Python35\lib\threading.py", line 914, in _bootstrap_inner 
    self.run() 
    File "C:\Python\Python35\lib\threading.py", line 862, in run 
    self._target(*self._args, **self._kwargs) 
    File "C:\Users\PATTY\PycharmProjects\CH15_TASKS\practice.py", line 25, in downloadXkcd 
    res = requests.get(comicUrl, "html.parser") 
    File "C:\Python\Python35\lib\site-packages\requests\api.py", line 70, in get 
    return request('get', url, params=params, **kwargs) 
    File "C:\Python\Python35\lib\site-packages\requests\api.py", line 56, in request 
    return session.request(method=method, url=url, **kwargs) 
    File "C:\Python\Python35\lib\site-packages\requests\sessions.py", line 461, in request 
    prep = self.prepare_request(req) 
    File "C:\Python\Python35\lib\site-packages\requests\sessions.py", line 394, in prepare_request 
    hooks=merge_hooks(request.hooks, self.hooks), 
    File "C:\Python\Python35\lib\site-packages\requests\models.py", line 294, in prepare 
    self.prepare_url(url, params) 
    File "C:\Python\Python35\lib\site-packages\requests\models.py", line 354, in prepare_url 
    raise MissingSchema(error) 
requests.exceptions.MissingSchema: Invalid URL '//imgs.xkcd.com/comics/family_circus.jpg': No schema supplied. Perhaps you meant http:////imgs.xkcd.com/comics/family_circus.jpg? 

をURLが無効であるが、私はwebrowserにURLペーストをコピーするたびに、それが思われること言います有効である。誰でも私はこれを修正する方法を知っていますか?ありがとう

+5

あなたのURLを修正してください。あなたのブラウザがあなたのためにそれを修正しただけで、それは有効にしません。 – spectras

+3

''属性の 'src'タグに' http:// 'や' https:// 'が指定されていないという問題があります。これはブラウザで有効で、' requests'では有効ではありません。 http://stackoverflow.com/questions/30770213/no-schema-supplied-and-other-errors-with-using-requests-getを参照してください。 – charlierproctor

+0

ありがとうございました。 – tadm123

答えて

2

あなたのURLがあなたのURLを修正したからといって、@spectrasと同じように、正当であるとは限りません。 「http://www」と入力してください。その前にそれが働いているかどうかを調べてみてください。

関連する問題