第一个爬图片的程序(xkcd.com)

import webbrowser, requests, bs4

#webbrowser.open("https://Xkcd.com")
number = 1
PreveUrl = 'https://Xkcd.com'

def downLoadImage(url,number):
    XkcdHtml = requests.get(url)   #  获取主页源码
    XkcdBs = bs4.BeautifulSoup(XkcdHtml.text, "html.parser")  #  获取bs4对象
    imgUrlList = XkcdBs.select('img')  #  选择img标签
    imageData = requests.get('http:'+imgUrlList[1].get('src'))  #  获取图片URL
    imageFile = open('image/'+str(number)+'.png', 'wb')   #  下载图片
    for i in imageData.iter_content(100000):
        imageFile.write(i)
    imageFile.close()
    preve = XkcdBs.select('a[rel]')
    preveUrl = "https://Xkcd.com" + preve[1].get('href')  #  拼接上一个页面的URL
    return preveUrl

while 1:
    PreveUrl = downLoadImage(PreveUrl,number)
    number += 1
    if PreveUrl.strip('/') == '1':
        break

书本上的答案:

url = 'https://xkcd.com'
os.makedirs('xkcd', exist_ok=True)
while not url.endswith('#'):
    #Todo: Download the page.
    print('Downloading page %s...' % url)
    res = requests.get(url)
    res.raise_for_status()

    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    #Todo:Find the URL of th comic image.
    comicElem = soup.select('#comic img')
    print(comicElem)
    if comicElem == []:
        print('Could not find comic image')
    else:
        comicUrl = 'http:' + comicElem[0].get('src')
        print('Dowunloading image %s...' % comicUrl)
        res = requests.get(comicUrl)
        res.raise_for_status()
    #Todo: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()

    #Todo: Get the Prev button's url.
    prevLink = soup.select('a[rel="prev"]')[0]
    url = 'http://xkcd.com' + prevLink.get('href')

print('Done.')

总结:爬取图片的步骤:

第一步:先获取网页的源码 res = requests.get(url),获取respond对象

第二步:将源码文本信息转换为 BeautifulSoup对象, soup = bs4.BeautifulSoup(res.text)

第三步:使用选择器筛选内容。(标签上一级有id ,筛选字符为‘#上以及标签ID 目标标签 。没有ID,则‘目标标签[属性+属性值]’’)

第四步:重新打开url,res=requests.get(url)

第五步:下载,res.iter_content(100000)

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值