推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
TKKONE
V2EX  ›  Python

你们想要的 Tumblr 爬虫

  •  
  •   TKKONE ·
    PRO
    · Oct 28, 2016 · 19360 views
    This topic created in 3666 days ago, the information mentioned may be changed or developed.

    好几个月前写的了,写的比较挫。
    并没有写成爬取一个博客的所有内容,本来是用来网站的,如果要爬所有内容,会让用户等待太久。

    # -*- coding=utf-8 -*-
    from threading import Thread
    import Queue
    import requests
    import re
    import os
    import sys
    import time
    
    
    api_url='http://%s.tumblr.com/api/read?&num=50&start='
    UQueue=Queue.Queue()
    def getpost(uid,queue):
        url='http://%s.tumblr.com/api/read?&num=50'%uid
        page=requests.get(url).content
        total=re.findall('<posts start="0" total="(.*?)">',page)[0]
        total=int(total)
        a=[i*50 for i in range(1000) if i*50-total<0]
        ul=api_url%uid
        for i in a:
            queue.put(ul+str(i))
    
    
    extractpicre = re.compile(r'(?<=<photo-url max-width="1280">).+?(?=</photo-url>)',flags=re.S)   #search for url of maxium size of a picture, which starts with '<photo-url max-width="1280">' and ends with '</photo-url>'
    extractvideore=re.compile('/tumblr_(.*?)" type="video/mp4"')
    
    video_links = []
    pic_links = []
    vhead = 'https://vt.tumblr.com/tumblr_%s.mp4'
    
    class Consumer(Thread):
    
        def __init__(self, l_queue):
            super(Consumer,self).__init__()
            self.queue = l_queue
    
        def run(self):
            session = requests.Session()
            while 1:
                link = self.queue.get()
                print 'start parse post: ' + link
                try:
                    content = session.get(link).content
                    videos = extractvideore.findall(content)
                    video_links.extend([vhead % v for v in videos])
                    pic_links.extend(extractpicre.findall(content))
                except:
                    print 'url: %s parse failed\n' % link
                if self.queue.empty():
                    break
    
    
    def main():
        task=[]
        for i in range(min(10,UQueue.qsize())):
            t=Consumer(UQueue)
            task.append(t)
        for t in task:
            t.start()
        for t in task:
            t.join
        while 1:
            for t in task:
                if t.is_alive():
                    continue
                else:
                    task.remove(t)
            if len(task)==0:
                break
    
    
    def write():
        videos=[i.replace('/480','') for i in video_links]
        pictures=pic_links
        with open('pictures.txt','w') as f:
            for i in pictures:
                f.write('%s\n'%i)
        with open('videos.txt','w') as f:
            for i in videos:
                f.write('%s\n'%i)
    
    
    if __name__=='__main__':
        #name=sys.argv[1]
        #name=name.strip()
        name='mzcyx2011'
        getpost(name,UQueue)
        main()
        write()
    
    Supplement 1  ·  Oct 29, 2016
    用法是:
    直接改掉那个 name 就行
    Supplement 2  ·  Oct 31, 2016
    我不会提供 name list 的,不然会被关小黑屋的
    52 replies  •  2018-09-22 18:22:54 +08:00
    miketeam
    miketeam
    Oct 28, 2016 via iPhone
    1
    Mark
    TKKONE
    TKKONE
    OP
    PRO
    Oct 29, 2016❤️1
    2
    忘了去重了!在 write 函数里面
    videos=list(set(videos))
    pictures=list(set(pictures))
    sammiriam
    sammiriam
    Oct 29, 2016
    3
    mark ,明天起来再看
    wjm2038
    wjm2038
    Oct 29, 2016 via Android
    4
    mark
    weipang
    weipang
    Oct 29, 2016 via iPhone
    5
    然而不会用
    TKKONE
    TKKONE
    OP
    PRO
    Oct 29, 2016 via iPhone
    7
    @cszhiyue Python 下载没多少意义,下载起来慢。所以我是写出文件,可以用迅雷下载
    aksoft
    aksoft
    Oct 29, 2016
    8
    刚需啊,出售营养快线!
    TKKONE
    TKKONE
    OP
    PRO
    Oct 29, 2016 via iPhone
    9
    @weipang 改个 name 就够了,然后直接运行
    TKKONE
    TKKONE
    OP
    PRO
    Oct 29, 2016 via iPhone
    10
    @aksoft 个人网站上目前有 5000 多个解析过的博客😝
    liuxingou
    liuxingou
    Oct 29, 2016
    11
    @tumbzzc

    正解,解析出地址,让下载工具下载,最高效率了。
    aksoft
    aksoft
    Oct 29, 2016
    12
    @tumbzzc 哪呢
    programdog
    programdog
    Oct 29, 2016
    13
    感谢楼主
    TKKONE
    TKKONE
    OP
    PRO
    Oct 29, 2016 via iPhone
    14
    @aksoft 最下面
    freaks
    freaks
    Oct 29, 2016 via Android
    15
    这样的在线解析不要太多(⊙o⊙)哦!😂
    0915240
    0915240
    Oct 29, 2016
    16
    olddrivertaketakeme
    Nicksxs
    Nicksxs
    Oct 29, 2016
    17
    不是被墙了么, vps 上下吗
    cszhiyue
    cszhiyue
    Oct 29, 2016
    18
    @tumbzzc 开了 8 进程下载并不觉得慢啊。 是什么理由导致慢呢?
    exoticknight
    exoticknight
    Oct 29, 2016
    19
    这东西是好,但是我觉得爬出提供资源的 tumblr 名字更重要
    TKKONE
    TKKONE
    OP
    PRO
    Oct 29, 2016 via iPhone
    20
    @freaks 我的网站放在过外 vps 上,也是在线解析
    TKKONE
    TKKONE
    OP
    PRO
    Oct 29, 2016 via iPhone
    21
    @exoticknight 名字没办法
    guokeke
    guokeke
    Oct 29, 2016 via Android
    22
    Mark
    cevincheung
    cevincheung
    Oct 29, 2016
    23
    然后就可以 wget 了?
    exalex
    exalex
    Oct 29, 2016
    24
    能不能简述下爬虫效果。。。
    guonning
    guonning
    Oct 29, 2016 via Android
    25
    收藏了
    LeoEatle
    LeoEatle
    Oct 29, 2016
    26
    name 改成什么好,能否给个名单: )
    yangonee
    yangonee
    Oct 29, 2016
    27
    求 name_list
    lycos
    lycos
    Oct 29, 2016 via iPad
    28
    mark
    leetom
    leetom
    Oct 30, 2016
    29
    @cszhiyue

    下载到一半会这样

    Traceback (most recent call last):
    File "turmla.py", line 150, in <module>
    for square in tqdm(pool.imap_unordered(download_base_dir, urls), total=len(urls)):
    File "/home/leetom/.pyenv/versions/2.7.10/lib/python2.7/site-packages/tqdm/_tqdm.py", line 713, in __iter__
    for obj in iterable:
    File "/home/leetom/.pyenv/versions/2.7.10/lib/python2.7/multiprocessing/pool.py", line 668, in next
    raise value
    Exception: Unexpected response.
    thinks
    thinks
    Oct 30, 2016 via Android
    30
    Mark ,哎,老司机一言不合就发车啊。
    sangmong
    sangmong
    Oct 30, 2016
    31
    mark
    errorlife
    errorlife
    Oct 31, 2016
    32
    没人知道 www.tumblrget.com 吗
    mozutaba
    mozutaba
    Oct 31, 2016
    33
    @errorlife 无效啊
    errorlife
    errorlife
    Oct 31, 2016
    34
    @mozutaba 上梯子=。=
    Nutlee
    Nutlee
    Oct 31, 2016
    35
    战略 Mark
    iewgnaw
    iewgnaw
    Oct 31, 2016
    36
    不是有现成的 API 吗
    TKKONE
    TKKONE
    OP
    PRO
    Oct 31, 2016
    37
    @iewgnaw 这不就是用 api 吗
    znoodl
    znoodl
    Oct 31, 2016 via iPhone
    38
    我也用 golang 爬过。。。后来被墙就没搞了
    Layne
    Layne
    Oct 31, 2016
    39
    默默点个赞 :)
    itqls
    itqls
    Oct 31, 2016
    40
    一天到晚搞事情
    weaming
    weaming
    Oct 31, 2016
    41
    搞事搞事
    TKKONE
    TKKONE
    OP
    PRO
    Oct 31, 2016
    42
    @itqls
    @weaming
    你们别搞事啊
    GreatMartial
    GreatMartial
    Nov 1, 2016 via Android
    43
    @tumbzzc 楼主,我要访问你的网站,我要做的你粉丝😄
    TKKONE
    TKKONE
    OP
    PRO
    Nov 1, 2016
    44
    @GreatMartial 少儿不宜哈哈哈
    firefox12
    firefox12
    Nov 1, 2016
    45
    下载的那个脚本
    Traceback (most recent call last):
    File "./1.py", line 138, in <module>
    getpost(name, UQueue)
    File "./1.py", line 27, in getpost
    total = re.findall('<posts start="0" total="(.*?)">', page)[0]
    IndexError: list index out of range
    Doggy
    Doggy
    Nov 5, 2016
    46
    with open('pictures.txt','r') as fobj:
    for eachline in fobj:
    pngurl=eachline.strip()
    filename='.//getpic//test-{0}.jpg'.format(i)
    print '[-]parsing:{0}'.format(filename)
    urllib.urlretrieve(pngurl,filename)
    i+=1
    dickeny
    dickeny
    Nov 6, 2016
    47
    for i in range(0, total, 50): queue.put(ul+str(i))
    hard2reg
    hard2reg
    Nov 27, 2016
    48
    看完表示自己 python 白学了。。。
    人家的爬虫都是多线程,队列,类
    我的爬虫都是。。。 while if for ....
    pyufftj
    pyufftj
    Feb 2, 2017
    49
    @hard2reg 多线程是为了提高速度, 3 个小时的事情, 1 个小时就做完了,多爽啊!
    dr3am
    dr3am
    Oct 31, 2017
    50
    求 LZ 网站
    TKKONE
    TKKONE
    OP
    PRO
    Nov 1, 2017
    51
    @dr3am #50 不可告人
    giveupAK47
    giveupAK47
    Sep 22, 2018
    52
    请问老哥您的博客是什么?想深入学习一下爬虫。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2807 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 106ms · UTC 05:13 · PVG 13:13 · LAX 21:13 · JFK 00:13
    ♥ Do have faith in what you're doing.