Python中的多线程

2024-05-02 22:58:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在遵循“用Python自动化无聊的任务”这本书,并试图创建一个程序,从http://xkcd.com下载多个漫画 同时,也遇到了一些问题。我复制的程序和书上的完全一样。在

我的代码是:

# 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.')

我得到了以下例外:

^{pr2}$

据说这个URL是无效的,但是每当我复制粘贴这个URL到webrowser中时,它似乎是有效的。有人知道我该怎么解决这个问题吗?谢谢


Tags: theinimagecomhttpforosres
1条回答
网友
1楼 · 发布于 2024-05-02 22:58:37

是的,正如@spectras所说,仅仅因为你的url修复了你的url并不意味着它是有效的。 试着在它前面加上“http://www.”,看看它是否有效。在

相关问题 更多 >