http://cuiqingcai.com/3179.html

 # *-* coding: UTF-8 *-*
import urllib2
import cookielib
import re
import time
import os ####################################
#cookie
cookie = cookielib.CookieJar()
handler = urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(handler)
#####
def mkdir(path):
path = path.strip()
# 判断路径是否存在
# 存在 True
# 不存在 Flase
isExists = os.path.exists(path)
if not isExists:
print u'新建了名字叫做',path,u'的文件夹'
# 创建目录操作函数
os.makedirs(path)
return True
else:
# 如果目录存在则不创建,并提示目录已经存在
print u'名为',path,u'的文件夹已经创建成功'
return False
#####
def saveImages(imglist,name):
print u'共 %s张图片' %len(imglist)
number = 1
for imageURL in imglist:
fileName = name + "/" + str(number) + ".jpg"
# 对于每张图片地址,进行保存
try:
u = urllib2.urlopen(imageURL, timeout = 10)
print ''
data = u.read()
print ''
f = open(fileName,'wb+')
print ''
f.write(data)
print u'正在保存的一张图片为',fileName
f.close()
except Exception, e:
print Exception,":",e
break
#保存封面,大图一般太大,截图过大,容易超时
number += 1
#####
if __name__ == '__main__':
patter = r'<span id="thread_(\d{7})">'
reg = re.compile(patter)
for i in range(1, 5):#前5页
if i%10 == 0: print 'now is %s' %i
req = urllib2.Request('http://38.103.161.179/forum/forumdisplay.php?fid=230&filter=type&typeid=172&page=%s' %str(i))
html = unicode(opener.open(req).read(), 'gbk')
tar = reg.findall(html) for jpos, j in enumerate(tar):#遍历所有子链接
req2 = urllib2.Request('http://38.103.161.179/forum/viewthread.php?tid='+j)
html2 = unicode(opener.open(req2).read(), 'gbk')
endpos = html2.index(u'附件</h4>')
stapos = html2.index(u'格式')
html2 = html2[stapos:endpos]
#print html2
patter2 = r'src="(.+?\.jpg)"'
reg2 = re.compile(patter2)
tar2 = reg2.findall(html2)
path = u'图集'+str(i)+u'之图'+str(jpos)
mkdir(path)
saveImages(tar2, path)
exit()

感谢weiyinfu学长指出。

urllib2用requests替代。

解析页面re用BeautifulSoup替代。

scrapy框架。

遇到验证码,用PIL,opencv,pybrain等。

多线程threading,python并行库框架celery。

Crawl(2)的更多相关文章

  1. How Google TestsSoftware - Crawl, walk, run.

    One of the key ways Google achievesgood results with fewer testers than many companies is that we ra ...

  2. SharePoint Error - An unrecognized HTTP response was received when attempting to crawl this item

    SharePoint 2013爬网报错 An unrecognized HTTP response was received when attempting to crawl this item. V ...

  3. Creating a SharePoint BCS .NET Connectivity Assembly to Crawl RSS Data in Visual Studio 2010

    from:http://blog.tallan.com/2012/07/18/creating-a-sharepoint-bcs-net-assembly-connector-to-crawl-rss ...

  4. SharePoint Search之(两)持续抓取Continues crawl

    于SharePoint 2010与在先前的版本号.有两种类型的抓取,Full和Incremental.故名思议.Full Crawl 抓取的时间.该Content Source里面的内容再次攀升.In ...

  5. scrapy crawl 源码修改 爬虫多开

    import os from scrapy.commands import ScrapyCommand from scrapy.utils.conf import arglist_to_dict fr ...

  6. Scrapy Crawl 运行出错 AttributeError: 'xxxSpider' object has no attribute '_rules' 的问题解决

    按照官方的文档写的demo,只是多了个init函数,最终执行时提示没有_rules这个属性的错误日志如下: ...... File "C:\ProgramData\Anaconda3\lib ...

  7. 21天打造分布式爬虫-Crawl类爬取小程序社区(八)

    8.1.Crawl的用法实战 新建项目 scrapy startproject wxapp scrapy genspider -t crawl wxapp_spider "wxapp-uni ...

  8. 运行scrapy crawl (文件名)时显示invalid syntax和no modle 'win32api'解决方案

    使用pycharm爬取知乎网站的时候,在terminal端输入scarpy crawl zhihu,提示语法错误,如下: 原因是python3.7中将async设为关键字,根据错误提示,找到manho ...

  9. Python.错误解决:scrapy 没有crawl 命令

    确保2点: 1.把爬虫.py复制到spiders文件夹里 如执行scrapy crawl demo ,spiders里面就要有demo.py文件 2.在项目文件夹内执行命令 在scrapy.cfg所在 ...

  10. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl 1.函数调用它自身,这样就形成了一个循环,一环套一环: from urllib.request ...

随机推荐

  1. ThreeJS实现波纹粒子效果

    今天我们来用ThreeJS的库实现一个波纹粒子效果,我们用到的ThreeJS的库有CanvasRenderer.js,OrbitControls.js,Projector.js,stats.min.j ...

  2. [C++]模板类和模板函数

    参考: C++ 中模板使用详解 C++模板详解 概念 为了避免因重载函数定义不全面而带来的调用错误,引入了模板机制 定义 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模 ...

  3. SQL Server 各版本安装包分享

    已将SQL Server 2005以上各版本的安装包分享到百度云盘,有需要的朋友可以下载进行安装,相关安装教程可以百度搜索.安装遇到难以解决的问题可以留言给我,2016版以上在选择功能的时候建议初学者 ...

  4. 监控与管理-SpringBoot

    在微服务架构中,我们将原本庞大的单体系统拆分成多个提供不同服务的应用. 虽然 各个应用的内部逻辑因分解而得以简化,但是由于部署应用的数量成倍增长,使得系统的 维护复杂度大大提升. 对于运维人员来说,随 ...

  5. spring boot 配置全局日期类型转换器

    1. 首先自定义一个类型转换器 import org.springframework.core.convert.converter.Converter; import org.springframew ...

  6. python打印对象的所有可操作内容

    print('\n'.join(['%s:%s' % item for item in 对象.__dict__.items()]))

  7. ats 分层缓存

    了解缓存层次结构 缓存层次结构由彼此通信的缓存级别组成.ats支持多种类型的缓存层次结构. 所有缓存层次结构都识别父和子的概念. 父缓存是层次结构中较高的缓存, ats可以 将请求转发到该缓存.子缓存 ...

  8. ef5 数据库操作

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. 工程能力之C4模型

    概述 刚在InfoQ上看到一篇介绍C4Model的文章,觉得这个模型设计的很赞,很有指导意义,做个简单的记录. Why,为什么需要架构图? ThoughtWorks中国 文章中有几句话我觉得很有道理, ...

  10. getField()与getDeclaredField()的区别

    Java的反射机制中,用Class的getField(String name)或getDelaredField(String name)可以得到目标类的指定属性,返回类型是Field. 但这两个是有区 ...