我的脚本没有检索列表的项。如果有人能帮我理解原因,我会很感激的?

2024-05-02 20:55:15 发布

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

我被要求更改AtBS书中的xkcd项目,以便从其他网站下载漫画。这是我的剧本

#! python3
# getwebcomic.py - Downloads every single smbc comic.

import requests, os, bs4

os.chdir('C:\\Users\\Bob\\Desktop\\')
url = 'https://smbc-comics.com' # starting url
os.makedirs('smbc', exist_ok=True) # store comics in ./smbc
noAbuse=0

for noAbuse in range(0, 5):
#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, "html.parser")

    # Find the URL of the comic image.
    comicElem = soup.select('#cc-comicbody')
    print('I am finding it')
    print(comicElem)
    if comicElem == []:
        print('Could not find comic image.')
    else:
        print(comicElem[0].get('src'))
        print('I dont know why the .get is returning NONE!')
        print('It is there???')
        print('...and now it crashes')
        comicUrl = 'https//smbc-comics.com' + comicElem[0].get('src')
        print(comicUrl)
        # Download the image.

        print('Downloading image %s...' % (comicUrl))
        res = requests.get(comicUrl)
        res.raise_for_status()

        # Save the image to ./smbc
        imageFile = open(os.path.join('smbc', 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 = 'http://smbc-comic.com' + prevLink.get('href')

print('Done.')

输出

Downloading page https://smbc-comics.com...

I am finding it
[<div id="cc-comicbody"><img border="0" id="cc-comic" 
 src="/comics/1524150658-20170419 (1).png" 
 title="You can also just use an infinite quantity of compasses as on-off switches."/><br/></div>]
None

我不知道为什么.get没有返回! 它在那里??? …现在它崩溃了

Traceback (most recent call last):
File "C:\Users\Bob\PythonScripts\getwebcomic.py", line 31, in <module>
comicUrl = 'https//smbc-comics.com' + comicElem[0].get('src')
TypeError: must be str, not NoneType

我似乎不明白为什么.get方法在存在'src'属性时返回none。如有任何提示,将不胜感激。我添加了一些额外的print(),以帮助查看脚本运行时发生的情况


Tags: thehttpsimagesrccomurlgetos
1条回答
网友
1楼 · 发布于 2024-05-02 20:55:15

comicElem[0]是一个除法(<div>)。它没有src属性,这就是.get返回None的原因。您应该尝试comicElem[0].img.get("src"),它返回"/comics/1524150658-20170419 (1).png"

相关问题 更多 >