Python学习之爬虫(小甲鱼)

依葫芦画瓢 

用字符串查找图片地址下载 

图片放在当前目录 

GIF下载下来不会动.....

 

 

import urllib.request
import time

def open_url(url):
    #return htmlpage
    print(url)
    req = urllib.request.Request(url)
    req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
    response = urllib.request.urlopen(req)
    return response.read()

def getInitialpage():
    #return how many pages we have
    url = "http://jandan.net/ooxx"
    html = open_url(url)
    html = html.decode("utf-8")
    index = html.find("span class=\"current-comment-page\"")
    beginindex = html.find("[" , index)
    endindex = html.find("]" , index)
    initialpage = html[(beginindex+1) : endindex]
    return initialpage

def getpiclist(pageurl):
    html = open_url(pageurl)
    html = html.decode("utf-8")
    piclist = list()
    for i in range(html.count("[查看原图]</a><br /><img")):
        index = html.find("[查看原图]</a><br /><img")
        html=html[index:]
        beginindex = html.find("\"")
        endindex = html.find("\"" , (beginindex+1))
        picurl = html[beginindex+1:endindex]
        html = html[endindex:]
        piclist.append(picurl)
    return piclist

def savepic(piclist):
    for picurl in piclist:
        html = open_url("http:{}".format(picurl))
        filename = picurl.split("/")[-1]
        print(filename)
        with open(filename , "wb") as f:
            f.write(html)
        time.sleep(1)

def test(page):
    initialpage = int(getInitialpage())
    for i in range((initialpage-page),(initialpage+1)):
        pageurl = "http://jandan.net/ooxx/page-{}#comments".format(i)
        piclist = getpiclist(pageurl)
        savepic(piclist)        
if __name__ == "__main__":
    test(1)

补充:

request库应该有一个retrieve方法用于下载,可以替换上述的 savepic() 中的代码,动图可正常显示

 

 

 

 

  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值