把无聊的东西自动化第11章第3步

2024-05-02 22:34:59 发布

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

在这个错误出现之前,我已经设法解决了这些错误,并且我已经完全按照章节中的说明复制了代码,但是我发现这个错误被抛出,我似乎无法修复它:

File "xkcd.py", line 34
continue
^
SyntaxError: 'continue' not properly in loop.

这是我的代码:

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

我理解显而易见的错误,因为错误是非常清楚的,但是作为一个从网站直接复制代码的新手,我以前没有遇到过这种事情,我很难理解问题到底在哪里。在


Tags: 代码imagehttpget错误notresselect