zoukankan      html  css  js  c++  java
  • 17173图片库“分布式”爬取

    低端好用爬虫分布式代码   代码比较low 请各位多多指点

    直接复制粘贴就(you)能(cuo)用(wu)的(o)!  

     

    首先分布式网上大多数的参考资料是坑     有的是不全,有的是有错误  总之看来看去头痛

    还是找一个大牛指点一二比较好。

    其次要有专研代码的精神  不弄明白闹心的习惯。 

    最后实在弄不明白没办法了  呵呵

    说明下一这个分布式的机制
    状态status1=1 为开始
    状态status2=2 为下载过程
    状态status3=3 为完成下载
    当状态status2=2 下载过程中终止下载 那么点击继续下载 代码会在mongo数据库里面
    会直接找寻状态status1=1  不会去找状态status2=2  状态status2=2会始终保持状态status2=2直到启动“17173图片分布式03.py”补丁代码
    17173图片分布式01.py
     1 #encoding:utf8
     2 from selenium import webdriver
     3 from selenium.webdriver.support.ui import WebDriverWait
     4 import requests
     5 import os
     6 import re
     7 from lxml import etree
     8 import time
     9 import json
    10 import hashlib
    11 import pymongo
    12 #说明下一这个分布式的机制
    13 #状态status1=1 为开始
    14 #状态status2=2 为下载过程
    15 #状态status3=3 为完成下载
    16 #当状态status2=2 下载过程中终止下载 那么点击继续下载 代码会在mongo数据库里面
    17 #找寻状态status1=1  不会去找状态status2=2  状态status2=2会始终保持状态status2=2直到启动“17173图片分布式03.py”补丁代码
    18 status1=1
    19 status2=2
    20 status3=3
    21 first_one=pymongo.MongoClient()
    22 #链接mongo数据库
    23 mongo=first_one['step01']['one']
    24 #创建数据库为'step01',表明为'one'
    25 url=['http://tu.17173.com/album/detail-{number}.html#ad-image0'.format(number=number) for number in range(1,8870)]
    26 #所有图片链接
    27 for i in url:
    28     data={'_id':i,'status':1}
    29     #把链接作为id  给状态status1=1
    30     try:
    31         mongo.insert(data)
    32         #入库
    33     except:
    34         print(i,'存在')
    17173图片分布式02.py
     1 #encoding:utf8
     2 from selenium import webdriver
     3 from selenium.webdriver.support.ui import WebDriverWait
     4 import requests
     5 import os
     6 import re
     7 from lxml import etree
     8 import time
     9 import json
    10 import hashlib
    11 import pymongo
    12 status1=1
    13 status2=2
    14 status3=3
    15 second_two=pymongo.MongoClient()
    16 #链接mongo数据库
    17 mongo_one=second_two['step01']['one']
    18 #创建数据库为'step01',表明为'one'
    19 zhi=mongo_one.find({'status':1})
    20 #在mongo里找状态status1=1
    21 for ten in zhi:
    22     cun = mongo_one.find_and_modify(
    23         query={'_id': ten['_id']},
    24         #查询id
    25         update={'$set': {'status': 2, }}
    26         #把状态status1=1 升级为状态status2=2
    27     )
    28     first_url = ten['_id']
    29     #给取出来的id赋予一个变量first_url
    30     first=requests.get(first_url).content.decode('utf-8')
    31     #打开链接
    32     op=open('临时.txt','w+',encoding='utf-8')
    33     op.write(first)
    34     op.close()
    35     #存入一个"临时.txt"里  可以不写此代码
    36     take=open('临时.txt','r',encoding='utf-8')
    37     have_look=take.read()
    38     take.close()
    39     # 存入一个"临时.txt"里  可以不写此代码
    40     second = re.compile('"originImgPath":"(.*?)"', re.S)
    41     #正则
    42     three = second.findall(have_look)
    43     #获取
    44     if three == []:
    45         #如果获取为空
    46         continue
    47         #跳过
    48     else:
    49         # print(three)
    50         for i in three:
    51 
    52             md5 = hashlib.md5()
    53             data = str(i)
    54             md5.update(data.encode('utf8'))
    55             name_pic = md5.hexdigest()
    56             # 用方法md5给图片命名
    57             # 图片的名字
    58             onepath = 'D:\17173图片'
    59             twopath = 'D:\17173图片' + '\' + name_pic + '.jpg'
    60 
    61             if os.path.exists(onepath):
    62                 print('D:17173图片路径存在')
    63             else:
    64                 os.makedirs('D:\17173图片')
    65             if os.path.exists(twopath):
    66                 print('图片' + name_pic + '存在')
    67             else:
    68                 print('图片' + name_pic + '正在下载')
    69                 op_pic = open(twopath, 'wb')
    70                 url_finish = i.replace('\', '')
    71                 #处理一下链接
    72                 content_url = requests.get(url_finish).content
    73                 #形成二进制下载
    74                 op_pic.write(content_url)
    75                 #写入
    76                 op_pic.close()
    77                 #关闭
    78 
    79         finish = mongo_one.find_and_modify(
    80             query={'_id': ten['_id']},
    81             #查询id
    82             update={'$set': {'status': 3, }}
    83             #把状态status2=2 升级为状态status3=3
    84         )
    85         print('-------------------')
    86     print(ten)
    17173图片分布式03.py
     1 #encoding:utf8
     2 from selenium import webdriver
     3 from selenium.webdriver.support.ui import WebDriverWait
     4 import requests
     5 import os
     6 import re
     7 from lxml import etree
     8 import time
     9 import json
    10 import hashlib
    11 import pymongo
    12 
    13 third_three=pymongo.MongoClient()
    14 mongo_one=third_three['step01']['one']
    15 zhi=mongo_one.find({'status':2})
    16 #在mongo里找状态status2=2
    17 #同理“17173图片分布式02.py"
    18 for ten in zhi:
    19     cun = mongo_one.find_and_modify(
    20         query={'_id': ten['_id']},
    21         update={'$set': {'status': 2, }}
    22     )
    23     first_url = ten['_id']
    24     first = requests.get(first_url).content.decode('utf-8')
    25     op = open('临时.txt', 'w+', encoding='utf-8')
    26     op.write(first)
    27     op.close()
    28 
    29     take = open('临时.txt', 'r', encoding='utf-8')
    30     aaa = take.read()
    31     take.close()
    32 
    33     second = re.compile('"originImgPath":"(.*?)"', re.S)
    34     three = second.findall(aaa)
    35     if three == []:
    36         continue
    37     else:
    38         # print(three)
    39         for i in three:
    40             # print(i)
    41             md5 = hashlib.md5()
    42             data = str(i)
    43             md5.update(data.encode('utf8'))
    44             name_pic = md5.hexdigest()
    45             # print(name_pic)
    46             # 名字
    47             onepath = 'D:\17173图片'
    48             twopath = 'D:\17173图片' + '\' + name_pic + '.jpg'
    49 
    50             if os.path.exists(onepath):
    51                 print('D:17173图片路径存在')
    52             else:
    53                 os.makedirs('D:\17173图片')
    54             if os.path.exists(twopath):
    55                 print('图片' + name_pic + '存在')
    56             else:
    57                 print('图片' + name_pic + '正在下载')
    58                 op_pic = open(twopath, 'wb')
    59                 url_finish = i.replace('\', '')
    60                 content_url = requests.get(url_finish).content
    61                 op_pic.write(content_url)
    62                 op_pic.close()
    63 
    64         finish = mongo_one.find_and_modify(
    65             query={'_id': ten['_id']},
    66             update={'$set': {'status': 3, }}
    67         )
    68         print('-------------------')
    69     print(ten)






























  • 相关阅读:
    3.nginx反向代理服务器+负载均衡
    2.nginx整合PHP
    nginx-location rewrite
    Nginx(一):安装
    修改host文件原理 localhost,127.0.0.1之间有什么区别
    一个IP绑定多个域名
    私有IP
    转:Hadoop和Spark的异同
    C#中Trim()、TrimStart()、TrimEnd()的用法
    Java操作redis
  • 原文地址:https://www.cnblogs.com/jizhenyong/p/7089645.html
Copyright © 2011-2022 走看看