Crawl(2)
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)的更多相关文章
- How Google TestsSoftware - Crawl, walk, run.
One of the key ways Google achievesgood results with fewer testers than many companies is that we ra ...
- 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 ...
- 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 ...
- SharePoint Search之(两)持续抓取Continues crawl
于SharePoint 2010与在先前的版本号.有两种类型的抓取,Full和Incremental.故名思议.Full Crawl 抓取的时间.该Content Source里面的内容再次攀升.In ...
- scrapy crawl 源码修改 爬虫多开
import os from scrapy.commands import ScrapyCommand from scrapy.utils.conf import arglist_to_dict fr ...
- Scrapy Crawl 运行出错 AttributeError: 'xxxSpider' object has no attribute '_rules' 的问题解决
按照官方的文档写的demo,只是多了个init函数,最终执行时提示没有_rules这个属性的错误日志如下: ...... File "C:\ProgramData\Anaconda3\lib ...
- 21天打造分布式爬虫-Crawl类爬取小程序社区(八)
8.1.Crawl的用法实战 新建项目 scrapy startproject wxapp scrapy genspider -t crawl wxapp_spider "wxapp-uni ...
- 运行scrapy crawl (文件名)时显示invalid syntax和no modle 'win32api'解决方案
使用pycharm爬取知乎网站的时候,在terminal端输入scarpy crawl zhihu,提示语法错误,如下: 原因是python3.7中将async设为关键字,根据错误提示,找到manho ...
- Python.错误解决:scrapy 没有crawl 命令
确保2点: 1.把爬虫.py复制到spiders文件夹里 如执行scrapy crawl demo ,spiders里面就要有demo.py文件 2.在项目文件夹内执行命令 在scrapy.cfg所在 ...
- 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl
阅读OReilly.Web.Scraping.with.Python.2015.6笔记---Crawl 1.函数调用它自身,这样就形成了一个循环,一环套一环: from urllib.request ...
随机推荐
- implode函数的升级版,将一个多维数组的值转化为字符串
/** * implode函数的升级版 * 将一个多维数组的值转化为字符串 * @param $glue * @param $data * @return string */function mult ...
- Shuffle Bags让你的随机不那么随机
前言 当我最初写游戏时,我经常使用标准Random()函数,然后写一堆if和else条件来我获得预期结果.如果结果不太好,我会写更多的条件进行过滤或者筛选,直到我觉得游戏变得有趣.最近我发现有更好的方 ...
- 5. 使用Flask蓝图(blueprint)
一直到现在都没有怎么写代码,可能更得比较慢. 作业回顾 先来看一下文章4的作业吧,使用logbook的时候,遇到了时区不对的情况.那么我们怎么去解决这个问题呢? 实际上logbook默认采用的是世界标 ...
- Ubuntu系统无法识别Logitech M590蓝牙鼠标的问题
参见 - https://blog.csdn.net/yh2869/article/details/73119018 亲测可用. 系统:ubuntu 16.04 64bit 现象:鼠标配对可以成功,但 ...
- Xiuno BBS 4.0 修改时间显示
修罗开源轻论坛程序 - Xiuno BBS 4.0Xiuno BBS 4.0 是一款轻论坛产品,前端基于 BootStrap 4.0.JQuery 3,后端基于 PHP/7 MySQL XCache/ ...
- 插件使用-HighChart
一.介绍 让数据可视化更简单,兼容 IE6+.完美支持移动端.图表类型丰富.方便快捷的 HTML5 交互性图表库. 官网(英):https://www.highcharts.com/download ...
- #1490 : Tree Restoration-(微软2017在线笔试)
输入n m km个数,表示每层的节点个数接下来m行是每层的节点,节点顺序是从左往右的k个叶子节点k*k个矩阵,表示叶子节点之间的距离 输出:每个节点的父亲节点编号,root节点是0 题解:1.很明显, ...
- C++ 函数 参数传递方式
用函数调用所给出的实参(实际参数,actual arguments)向函数定义给出的形参(形式参数,formal arguments)设置初始值的过程,叫做参数传递(Argument Passing) ...
- sublime编写markdownm
sublime编写markdownm 以前用有道云笔记,找了半天更改字体大小,结果还找不 到,那个字实在是太小了,像我这种有强迫症的患者,实 在受不了简约风格的有道云,所以上网找了与和诺插件并 受到一 ...
- 进阶系列(5)—— C#XML使用
一.XML介绍 XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境中跨 ...