其中用到urllib2模块和正则表达式模块。下面直接上代码:
[/code]
import urllib,urllib2,gevent
import re,time
from gevent import monkey
monkey.patch_all()
def geturllist(url):
url_list=[]
print url
s = urllib2.urlopen(url)
text = s.read()
html = re.search(r'<ol.*</ol>', text, re.S)
urls = re.finditer(r'<p><img src="(.+?)jpg" /></p>',html.group(),re.I)
for i in urls:
url=i.group(1).strip()+str("jpg")
url_list.append(url)
return url_list
def download(downurl):
name=str(time.time())[:-3]+""+re.sub('.+?/','',down_url)
print name
urllib.urlretrieve(down_url, "D:\\TEMP\\"+name)
def getpageurl():
page_list = []
for page in range(1,700):
url="http://jandan.net/ooxx/page-"+str(page)+"#comments"
#把生成的url加入到page_list中
page_list.append(url)
print page_list
return page_list
if name == 'main':
jobs = []
pageurl = getpageurl()[::-1]
for i in pageurl:
for (downurl) in geturllist(i):
jobs.append(gevent.spawn(download, downurl))
gevent.joinall(jobs)
[/code]
程序不长才45行,不是太难,大家可以研究下,这里我只是抛砖引玉,大家可以根据原理开发出其他的抓取程序,呵呵,自己想去吧。。。我就不多说了~~
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8