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. Java 中的接口

    接口概念 接口可以理解为一种特殊的类,由全局常量和公共抽象方法组成.类是一种具体实现体,而接口定义了某一些类所要遵守的规范,接口不必关心这些类的内部数据,也不关心这些类里方法的实现细节,它只规定这些类 ...

  2. SQL查询语句大全及其理解

    转自:https://www.cnblogs.com/1234abcd/p/5530314.html 一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删 ...

  3. Windows系统环境变量之path环境变量(Java, Python环境变量配置)

    系统: Windows10 path系统环境变量的作用: Windows和DOS操作系统中的path环境变量,当要求系统运行一个程序而没有告诉它程序所在的完整路径时,系统除了在当前目录下面寻找此程序外 ...

  4. ngxin 添加模块

    if test -n "$NGX_ADDONS"; then echo configuring additional modules for ngx_addon_dir in $N ...

  5. 高可用OpenStack(Queen版)集群-3.高可用配置(pacemaker&haproxy)

    参考文档: Install-guide:https://docs.openstack.org/install-guide/ OpenStack High Availability Guide:http ...

  6. Redis Jedis简介

    Redis是一种基于内存类型的数据存储工具 Jedis是一个用java写的Redis数据库操作的客户端,通过Jedis,可以很方便的对redis数据库进行操作.Jedis通过Jedis Pool进行R ...

  7. Apache 工作模式的正确配置

       prefork work event

  8. 基于spec评论作品

    组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶  刘佳瑞  公冶令鑫  杨磊  杨金铭  张宇  卢帝同 一.测试目标:拉格朗日2018——飞词 下面是他们的小游戏在运行时的一些截图画面: 1.开始: ...

  9. 实验1 熟悉Linux开发环境 实验报告

    参见http://www.cnblogs.com/lhc-java/p/4970269.html

  10. java BufferedWriter写数据不完全

    package com.brucekun.keyword; import java.io.BufferedReader; import java.io.BufferedWriter; import j ...