2006-11-09
出差时写的一个抓取baidu图片的东东
使用了多线程但没有处理同步。
正则表达式是用kodos弄出来的,这个工具使用起来比较顺手。
正则表达式是用kodos弄出来的,这个工具使用起来比较顺手。
#!/usr/bin/env python
#coding=cp936
import re
import urllib
import thread
import time
#url_part1 = '''http://images.google.cn/images?q='''
#url_part2 = '''&svnum=10&hl=zh-CN&lr=&newwindow=1&start='''
#url_part3 = '''&sa=N'''
url_part1 = '''http://image.baidu.com/i?z=0&cl=2&ct=201326592&sn=&lm=-1&cm=1&sc=0&bu=&rn=16&tn=baiduimage&word='''
url_part2 = '''&pn='''
def nextpage(keywords,start):
return url_part1 + urllib.quote(keywords) + url_part2 + str(start)
#compile_obj = re.compile(r'dyn.Img\("http://(.)+","","","')
compile_obj = re.compile(r'<a href="http://image.baidu.com/ir\?t=1&u=http://(.)+&f=http://')
def parseurl(content):
ret = []
match_objs = compile_obj.finditer(content)
for match in match_objs:
url = match.group().split('&')[1][2:]
ret.append(url)
return ret
#exitdic = {}
def downimg(url,exitdic):
filename = url.split('/')[-1]
#print url,'---->',filename
urllib.urlretrieve(url,filename)
exitdic[url].acquire()
def download(url):
exitdic = {}
print '开始下载页面: ' + url
conn = urllib.urlopen(url)
content = conn.read()
conn.close()
urls = parseurl(content)
for url in urls:
print url
exitdic[url] = thread.allocate_lock()
thread.start_new(downimg,(url,exitdic))
for key in exitdic.keys():
while not exitdic[key].locked():
#time.sleep(200)
pass
print '完成一页'
#抓取关键字为vista的图片,抓取范围为查询结果的第4到8页
if __name__ == '__main__':
for i in range(4,8):
url = nextpage('vista',i*16)
download(url)
print 'OK'
发表评论
- 浏览: 48965 次

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
JBPM 与 Spring 结合
sessionFactory怎么配的?
-- by jinguizi -
重新整理后的Oracle OAF学 ...
太好了!谢死你了!
-- by chenpu -
ExtJS中FormPanel实现数 ...
请教一下楼主,本地json数据如何实现分页?单击下一页时,怎样在js中得到当前的 ...
-- by gaipai -
重新整理后的Oracle OAF学 ...
这个格式的高亮显示都看不清了
-- by qinglangee -
ExtJS中FormPanel实现数 ...
把你的后台代码发出来看看啊 光说返回JSON的格式,看看你到底是怎么返回的啊 y ...
-- by yongtree






评论排行榜