├── .idea └── vcs.xml ├── LICENSE ├── README.md ├── run.py ├── scrapy.cfg └── xiuren ├── __init__.py ├── items.py ├── middleware ├── __init__.py ├── proxy_middleware.py └── randomuseragent_middleware.py ├── pipelines.py ├── settings.py └── spiders ├── __init__.py ├── gallery_spider.py ├── gank_spider.py ├── home_spiders.py ├── jcenter_spider.py └── meizi_spiders.py /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xiuren mzitu 2 | 秀人网爬虫 55156爬虫 mzitu 3 | 4 | 使用Python3.6的版本 5 | 6 | # 安装 7 | 8 | ``` 9 | pip install scrapy 10 | ``` 11 | 如果遇到有些模块在windos装不上,到[这个网站](http://www.lfd.uci.edu/~gohlke/pythonlibs/)下载相应的模块,然后用pip安装。 12 | 13 | # 运行 14 | ``` 15 | python run.py 16 | ``` 17 | 18 | 默认是爬取的55156 还可以在run.py里面去掉注释爬取mzitu 19 | 20 | # 结果 21 | ![noproxy.gif](http://upload-images.jianshu.io/upload_images/2189945-55d51000ca75c96a.gif?imageMogr2/auto-orient/strip) 22 | 23 | ![目录.png](http://upload-images.jianshu.io/upload_images/2189945-00f8ba822e11adfb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 24 | 25 | ![结果.png](http://upload-images.jianshu.io/upload_images/2189945-b8e824b837a505cc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | 4 | """ 5 | @author: su jian 6 | @contact: 121116111@qq.com 7 | @file: run.py 8 | @time: 2017/8/16 17:58 9 | """ 10 | 11 | from scrapy.cmdline import execute 12 | import os 13 | import sys 14 | sys.path.append(os.path.dirname(os.path.abspath(__file__))) 15 | # execute(['scrapy', 'crawl', 'xiuren_spider']) 16 | # execute(['scrapy', 'crawl', 'meizi_spider']) 17 | 18 | 19 | execute('scrapy crawl jcenter_spider'.split(' ')) 20 | 21 | # def func(): 22 | # sys.path.append(os.path.dirname(os.path.abspath(__file__))) 23 | # execute(['scrapy', 'crawl', 'xiuren_spider']) 24 | # 25 | # 26 | # if __name__ == '__main__': 27 | # func() 28 | 29 | # import re 30 | # print(re.findall(r'(\d+)', 'http://www.mzitu.com/57176')[0]) 31 | -------------------------------------------------------------------------------- /scrapy.cfg: -------------------------------------------------------------------------------- 1 | # Automatically created by: scrapy startproject 2 | # 3 | # For more information about the [deploy] section see: 4 | # https://scrapyd.readthedocs.org/en/latest/deploy.html 5 | 6 | [settings] 7 | default = xiuren.settings 8 | 9 | [deploy] 10 | #url = http://localhost:6800/ 11 | project = xiuren 12 | -------------------------------------------------------------------------------- /xiuren/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sujianqingfeng/scrapy_xiuren/a05d8fe05777aca26667c5b40573ef0431bcff06/xiuren/__init__.py -------------------------------------------------------------------------------- /xiuren/items.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Define here the models for your scraped items 4 | # 5 | # See documentation in: 6 | # http://doc.scrapy.org/en/latest/topics/items.html 7 | 8 | import scrapy 9 | 10 | 11 | class XiurenItem(scrapy.Item): 12 | image_url = scrapy.Field() 13 | image_dir = scrapy.Field() 14 | 15 | -------------------------------------------------------------------------------- /xiuren/middleware/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | 4 | """ 5 | @author: su jian 6 | @contact: 121116111@qq.com 7 | @file: __init__.py.py 8 | @time: 2017/8/16 17:43 9 | """ 10 | 11 | 12 | def func(): 13 | pass 14 | 15 | 16 | class Main(): 17 | def __init__(self): 18 | pass 19 | 20 | 21 | if __name__ == '__main__': 22 | pass -------------------------------------------------------------------------------- /xiuren/middleware/proxy_middleware.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import datetime 4 | import os 5 | # import random, requests, json 6 | 7 | 8 | class ProxyMiddleware(object): 9 | # overwrite process request 10 | def process_request(self, request, spider): 11 | # ip = '45.76.238.192:{}'.format(random.randint(8110, 8145)) 12 | 13 | 14 | # res = requests.get(url='http://127.0.0.1:6000/proxy/1.0.0/proxy', params={'site': 'ca'}) 15 | # ip_json = json.loads(res.text) 16 | # 17 | # ip = '{}:{}'.format(ip_json['ip'], ip_json['port']) 18 | # 19 | # spider.logger.info('代理ip 地址--->{}'.format(ip)) 20 | # request.meta['proxy'] = "http://{}".format(ip) 21 | pass 22 | 23 | 24 | # # 代理服务器 25 | # proxyServer = "http://proxy.abuyun.com:9020" 26 | # 27 | # # 代理隧道验证信息 28 | # proxyUser = "H4A7Z562648XD5GD" 29 | # proxyPass = "E67B2E17F8E52317" 30 | # 31 | # # for Python2 32 | # proxyAuth = "Basic " + base64.b64encode(proxyUser + ":" + proxyPass) 33 | # 34 | # request.meta["proxy"] = proxyServer 35 | # 36 | # request.headers["Proxy-Authorization"] = proxyAuth 37 | -------------------------------------------------------------------------------- /xiuren/middleware/randomuseragent_middleware.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*-coding:utf-8-*- 3 | import random 4 | import logging 5 | from scrapy.downloadermiddlewares.useragent import UserAgentMiddleware 6 | 7 | 8 | class RandomUserAgentMiddeleware(UserAgentMiddleware): 9 | user_agent_list = [ 10 | "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)", 11 | "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)", 12 | "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)", 13 | "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)", 14 | "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)", 15 | "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)", 16 | "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)", 17 | "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)", 18 | "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6", 19 | "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1", 20 | "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0", 21 | "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5", 22 | "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6", 23 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11", 24 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20", 25 | "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52", 26 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11", 27 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER", 28 | "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)", 29 | "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)", 30 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 LBBROWSER", 31 | "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)", 32 | "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)", 33 | "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)", 34 | "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)", 35 | "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)", 36 | "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)", 37 | "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1", 38 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1", 39 | "Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5", 40 | "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre", 41 | "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0", 42 | "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11", 43 | "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10" 44 | 45 | ] 46 | 47 | def __init__(self, user_agent=''): 48 | self.user_agent = user_agent 49 | 50 | def process_request(self, request, spider): 51 | ua = random.choice(self.user_agent_list) 52 | logging.info("当前UA----->" + ua) 53 | 54 | request.headers.setdefault('User-Agent', ua) 55 | -------------------------------------------------------------------------------- /xiuren/pipelines.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Define your item pipelines here 4 | # 5 | # Don't forget to add your pipeline to the ITEM_PIPELINES setting 6 | # See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html 7 | 8 | import os 9 | import scrapy 10 | from scrapy.pipelines.images import ImagesPipeline 11 | from scrapy.exceptions import DropItem 12 | 13 | 14 | class ImgSavePinpeline(ImagesPipeline): 15 | headers = { 16 | 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 17 | 'Accept-Encoding:': 'gzip, deflate', 18 | 'Accept-Language': 'en_US,en;q=0.8', 19 | 'Connection': 'keep-alive', 20 | 'Content-Type': 'application/x-www-form-urlencoded', 21 | 'Host': 'i.meizitu.net' 22 | } 23 | 24 | def get_media_requests(self, item, info): 25 | if info.spider.name == 'meizi_spider': 26 | yield scrapy.Request(item['image_url'], meta={'item': item}, headers=self.headers) 27 | else: 28 | yield scrapy.Request(item['image_url'], meta={'item': item}) 29 | 30 | def item_completed(self, results, item, info): 31 | image_paths = [x['path'] for ok, x in results if ok] 32 | 33 | if not image_paths: 34 | print('{}/{}------文件保存失败'.format(item['image_dir'], item['image_url'])) 35 | raise DropItem("Item contains no images") 36 | else: 37 | print('{}/{}------文件保存成功'.format(item['image_dir'], item['image_url'])) 38 | return item 39 | 40 | def file_path(self, request, response=None, info=None): 41 | item = request.meta['item'] 42 | base_name = os.path.basename(item['image_url']) 43 | index = item['image_dir'].find('(') 44 | if index == -1: 45 | index = item['image_dir'].find('(') 46 | if not index == -1: 47 | item['image_dir'] = item['image_dir'][:index] 48 | filename = u'{}/{}'.format(item['image_dir'], base_name) 49 | return filename 50 | -------------------------------------------------------------------------------- /xiuren/settings.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | BOT_NAME = 'xiuren' 5 | 6 | SPIDER_MODULES = ['xiuren.spiders'] 7 | NEWSPIDER_MODULE = 'xiuren.spiders' 8 | 9 | # Obey robots.txt rules 10 | ROBOTSTXT_OBEY = False 11 | 12 | # Configure maximum concurrent requests performed by Scrapy (default: 16) 13 | CONCURRENT_REQUESTS = 16 14 | 15 | DOWNLOAD_DELAY = 0 16 | 17 | # Disable cookies (enabled by default) 18 | COOKIES_ENABLED = False 19 | 20 | # 请求头 21 | DEFAULT_REQUEST_HEADERS = { 22 | 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 23 | 'Accept-Encoding:': 'gzip, deflate', 24 | 'Accept-Language': 'en_US,en;q=0.8', 25 | 'Connection': 'keep-alive', 26 | 'Content-Type': 'application/x-www-form-urlencoded' 27 | } 28 | 29 | DOWNLOADER_MIDDLEWARES = { 30 | 'xiuren.middleware.proxy_middleware.ProxyMiddleware': 100, 31 | 'xiuren.middleware.randomuseragent_middleware.RandomUserAgentMiddeleware': 101, 32 | } 33 | 34 | IMAGES_STORE = 'F:\\meizi' 35 | ITEM_PIPELINES = { 36 | 'xiuren.pipelines.ImgSavePinpeline': 300, 37 | } 38 | -------------------------------------------------------------------------------- /xiuren/spiders/__init__.py: -------------------------------------------------------------------------------- 1 | # This package will contain the spiders of your Scrapy project 2 | # 3 | # Please refer to the documentation for information on how to create and manage 4 | # your spiders. 5 | -------------------------------------------------------------------------------- /xiuren/spiders/gallery_spider.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | 4 | """ 5 | @author: sujian 6 | @contact: h121116111@gmail.com 7 | @file: gallery_spider.py 8 | @time: 2017/12/5 21:43 9 | """ 10 | 11 | import scrapy 12 | from scrapy import Request 13 | from xiuren.items import XiurenItem 14 | 15 | 16 | class GallerySpider(scrapy.Spider): 17 | name = "gallery_spider" 18 | 19 | start_urls = ['http://www.55156.com/weimeiyijing/fengjingtupian'] 20 | 21 | def parse(self, response): 22 | urls = response.xpath('//ul[@class="liL"]/li/a/@href').extract() 23 | 24 | self.logger.info('当前父图片urls----->{}'.format(urls)) 25 | for url in urls: 26 | yield Request(url=url, callback=self.img_parse) 27 | # yield Request(url='http://www.55156.com/weimeiyijing/fengjingtupian/146515.html', callback=self.img_parse) 28 | 29 | next_page = response.css('.pages ul li ::attr(href)')[-2].extract() 30 | if next_page and not next_page == '#': 31 | self.logger.info('存在列表下一页------>{}'.format(next_page)) 32 | yield Request(url=response.urljoin(next_page), callback=self.parse) 33 | 34 | def img_parse(self, respose): 35 | item = XiurenItem() 36 | img_url = respose.css('.articleBody p a img ::attr(src)').extract_first() 37 | img_dir = respose.css('.articleTitle h1 ::text').extract_first() 38 | 39 | item['image_url'] = img_url 40 | item['image_dir'] = img_dir 41 | 42 | next_page = respose.css('.pages ul li a ::attr(href)').extract() 43 | 44 | if next_page: 45 | next_page = next_page[-1] 46 | 47 | if next_page and not next_page == '#': 48 | self.logger.info('开始爬取图片下一页-------->{}'.format(next_page)) 49 | yield Request(url=respose.urljoin(next_page), callback=self.img_parse) 50 | 51 | yield item 52 | -------------------------------------------------------------------------------- /xiuren/spiders/gank_spider.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | 4 | """ 5 | @author: su jian 6 | @contact: 121116111@qq.com 7 | @file: gank_spider.py 8 | @time: 2017/12/5 16:43 9 | """ 10 | 11 | 12 | def func(): 13 | pass 14 | 15 | 16 | class Main(): 17 | def __init__(self): 18 | pass 19 | 20 | 21 | if __name__ == '__main__': 22 | pass -------------------------------------------------------------------------------- /xiuren/spiders/home_spiders.py: -------------------------------------------------------------------------------- 1 | import scrapy 2 | from xiuren.items import XiurenItem 3 | from scrapy import Request 4 | 5 | ''' 6 | http:+- 7 | 网站主页的爬虫 8 | ''' 9 | 10 | 11 | class XiuRenSpiders(scrapy.Spider): 12 | name = "xiuren_spider" 13 | 14 | start_urls = ["http://www.55156.com/gaoqingtaotu"] 15 | 16 | def parse(self, response): 17 | all_urls1 = response.xpath('//div[@class="labelbox_pic"]/ul/li/a/@href').extract() 18 | all_urls2 = response.xpath('//div[@class="columnt"]/span/a/@href').extract() 19 | all_urls3 = response.xpath('//div[@class="listBox"]/ul/li/a/@href').extract() 20 | all_urls = all_urls1 + all_urls2 + all_urls3 21 | all_urls = list(set(all_urls)) 22 | 23 | for url in all_urls: 24 | yield Request(response.urljoin(url), callback=self.parser_down) 25 | # yield Request(response.urljoin('http://www.55156.com/a/pans/2014/1217/4295.html'), 26 | # callback=self.parser_down) 27 | pass 28 | self.logger.info('当前页urls--{}'.format(all_urls)) 29 | 30 | next_page = response.xpath('//div[@class="pages"]/ul/li/a/@href').extract()[-2].replace('#', '') 31 | 32 | if next_page: 33 | yield Request(response.urljoin(next_page), callback=self.parse) 34 | self.logger.info('当前页抓取完毕--开始下一页--{}'.format(next_page)) 35 | 36 | def parser_down(self, response): 37 | 38 | item = XiurenItem() 39 | 40 | next_page = response.xpath('//div[@class="pages"]/ul/li/a/@href').extract()[-1].replace('#', '') 41 | img_link = response.xpath('//p[@align="center"]/a/img/@src').extract_first() 42 | img_dir = response.xpath('//div[@class="articleTitle"]/h1/text()').extract_first() 43 | 44 | item['image_dir'] = img_dir 45 | item['image_url'] = img_link 46 | self.logger.info('图片页面开始下载--{}--{}'.format(img_dir, img_link)) 47 | 48 | if next_page: 49 | yield Request(response.urljoin(next_page), callback=self.parser_down) 50 | 51 | yield item 52 | -------------------------------------------------------------------------------- /xiuren/spiders/jcenter_spider.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | 4 | """ 5 | @author: su jian 6 | @contact: 121116111@qq.com 7 | @file: jcenter_spider.py 8 | @time: 2018/3/27 15:38 9 | """ 10 | 11 | import scrapy 12 | from scrapy import Request 13 | 14 | 15 | class JcenterSpider(scrapy.Spider): 16 | name = "jcenter_spider" 17 | 18 | start_urls = ["http://jcenter.bintray.com/"] 19 | 20 | def parse(self, response): 21 | hrefs = response.xpath('//pre/a/@href').extract() 22 | 23 | for href in hrefs: 24 | if '/' in href: 25 | yield Request(url=response.url + str(href).replace(':', ''), callback=self.parse) 26 | else: 27 | self.logger.info('连接----->{}'.format(response.url + href.replace(':', ''))) 28 | -------------------------------------------------------------------------------- /xiuren/spiders/meizi_spiders.py: -------------------------------------------------------------------------------- 1 | import scrapy 2 | from xiuren.items import XiurenItem 3 | from scrapy import Request 4 | 5 | ''' 6 | http://www.mzitu.com/japan/ 7 | 8 | ''' 9 | 10 | import re 11 | 12 | 13 | class MeiziSpiders(scrapy.Spider): 14 | name = "meizi_spider" 15 | 16 | start_urls = ["http://www.mzitu.com/xinggan/", "http://www.mzitu.com/japan/", "http://www.mzitu.com/mm/", 17 | "http://www.mzitu.com/taiwan/"] 18 | 19 | custom_settings = { 20 | 21 | 'DEFAULT_REQUEST_HEADERS': { 22 | 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 23 | 'Accept-Encoding:': 'gzip, deflate', 24 | 'Accept-Language': 'en_US,en;q=0.8', 25 | 'Connection': 'keep-alive', 26 | 'Content-Type': 'application/x-www-form-urlencoded', 27 | 'Host': 'www.mzitu.com', 28 | 'Referer': 'http://www.mzitu.com' 29 | } 30 | 31 | } 32 | 33 | def parse(self, response): 34 | all_urls = response.xpath('//div[@class="postlist"]/ul/li/a/@href').extract() 35 | 36 | for url in all_urls: 37 | yield Request(response.urljoin(url), callback=self.parser_down) 38 | # yield Request(response.urljoin('http://www.mzitu.com/57176'), 39 | # callback=self.parser_down) 40 | # pass 41 | self.logger.info('当前页urls--{}'.format(all_urls)) 42 | 43 | next_page = response.css('a[class*=next] ::attr(href)').extract_first() 44 | 45 | if next_page: 46 | yield Request(response.urljoin(next_page), callback=self.parse) 47 | self.logger.info('当前页抓取完毕--开始下一页--{}'.format(next_page)) 48 | pass 49 | 50 | def parser_down(self, response): 51 | 52 | item = XiurenItem() 53 | 54 | next_page = response.xpath('//div[@class="pagenavi"]/a/@href').extract()[-1].replace('#', '') 55 | img_link = response.xpath('//div[@class="main-image"]/p/a/img/@src').extract_first() 56 | img_dir = response.xpath('//h2[@class="main-title"]/text()').extract_first() 57 | 58 | item['image_dir'] = img_dir 59 | item['image_url'] = img_link 60 | self.logger.info('图片页面开始下载--{}--{}'.format(img_dir, img_link)) 61 | 62 | if next_page and re.findall(r'(\d+)', response.url)[0] == re.findall(r'(\d+)', next_page)[0]: 63 | yield Request(response.urljoin(next_page), callback=self.parser_down) 64 | 65 | yield item 66 | --------------------------------------------------------------------------------