你们想要的 Tumblr 爬虫

2016-10-28 23:58:30 +08:00
 tumbzzc

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

# -*- 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()
17995 次点击
所在节点    Python
52 条回复
weaming
2016-10-31 17:37:37 +08:00
搞事搞事
tumbzzc
2016-10-31 17:42:37 +08:00
@itqls
@weaming
你们别搞事啊
GreatMartial
2016-11-01 14:55:39 +08:00
@tumbzzc 楼主,我要访问你的网站,我要做的你粉丝😄
tumbzzc
2016-11-01 14:58:01 +08:00
@GreatMartial 少儿不宜哈哈哈
firefox12
2016-11-01 15:26:01 +08:00
下载的那个脚本
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
2016-11-05 10:54:13 +08:00
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
2016-11-06 22:05:57 +08:00
for i in range(0, total, 50): queue.put(ul+str(i))
hard2reg
2016-11-27 23:58:38 +08:00
看完表示自己 python 白学了。。。
人家的爬虫都是多线程,队列,类
我的爬虫都是。。。 while if for ....
pyufftj
2017-02-02 16:26:56 +08:00
@hard2reg 多线程是为了提高速度, 3 个小时的事情, 1 个小时就做完了,多爽啊!
dr3am
2017-10-31 17:43:43 +08:00
求 LZ 网站
tumbzzc
2017-11-01 11:08:08 +08:00
@dr3am #50 不可告人
giveupAK47
2018-09-22 18:22:54 +08:00
请问老哥您的博客是什么?想深入学习一下爬虫。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/316337

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX