图片爬取失败又不报错

问题描述:
想要爬取标题以及图片,但是图片却怎么都爬不出来也不报错,不知道问题在哪里
代码如下
运行结果如图所示

img

import requests
from bs4 import BeautifulSoup
import re
import os  
headers={"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"}
url="https://www.zhihu.com/billboard"
html=requests.get(url,headers=headers)
soup=BeautifulSoup(html.text,'lxml')
divs=soup.select('.HotList-item')
num=0
for div in divs:
    title=div.select(' .HotList-itemBody .HotList-itemTitle')[0].text 
    print('标题:',title)
    try:
        img=div.select(' .HotList-itemimgContainer img')[0]['src']
        print(img)
        
        if not os.path.exists('zhihu'):
            os.mkdir('zhihu')
        with open('zhihu/{}.jpg'.format(num),'wb') as f:
            f.write(requests.get(img).content)
        num+=1
    except:
        pass

样式区分大小的,注意大小写
img=div.select(' .HotList-itemimgContainer img')[0]['src']
==>
img=div.select(' .HotList-itemImgContainer img')[0]['src']

这里错了,修改成HotList-itemImgContainer就可以了

img

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632