玩法收藏/云服务器/ Python 操作 Redis

2017-04-12 23:57:52 +08:00
 liuliliuli2017

又在腾云阁发现一篇 Python 的爬虫文章,顺便存了。

收录待用,修改转载已取得腾讯云授权


节选:

...

这是用来下载美图网上 100 个页面的所有的图片

import requests
import re
import time
from redis import Redis
headers={ 'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36' }

def push_redis_list():
    r = Redis(host='10.66.149.8',port=6379,password='')
    for i in range(100):
        num = 5100+i;
        url ='http://www.meizitu.com/a/'+ str(num) +'.html'
        img_url = requests.get(url,timeout=30)
        #print img_url.text
        #time.sleep(10)
        img_url_list = re.findall('http://mm.howkuai.com/wp-content/uploads/201.*.jpg',img_url.text)
        print(img_url_list)
        for temp_img_url in img_url_list:
            l = len(re.findall('limg',temp_img_url))
            #print l
            if(l == 0):
                print("url: ",temp_img_url)
                r.lpush('meizitu',temp_img_url)
        print(r.llen('meizitu'))
    return 0

def get_big_img_url():
    r = Redis(host='10.66.149.8',port=6379,password='')
    while(1):
        try:
            url = r.lpop('meizitu')
            download(url)
            time.sleep(1)
            print(url)
        except:
            print("请求求发送失败重试")
            time.sleep(10)
            continue
    return 0

def download(url):
    try:
        r = requests.get(url,headers=headers,timeout = 50)
        name = int(time.time())
        f = open('./pic/'+str(name)+'.jpg','wb')
        f.write(r.content)
        f.close()
    except Exception as e:
        print(Exception,":",e)

if __name__ == '__main__':
    url = 'http://www.meizitu.com/a/list_1_'
    print("begin")
    push_redis_list()#开启则加任务队列
    #get_big_img_url()#开启则运行爬取任务

...


原文链接: https://www.qcloud.com/community/article/337567001488804157

1937 次点击
所在节点    Python
0 条回复

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

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

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

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

© 2021 V2EX