一个简单的Python网络爬虫(抓图),针对某论坛.

时间:2021-07-11 04:10:44
 #coding:utf-8
import urllib2
import re
import threading #图片下载
def loadImg(addr,x,y,artName):
data = urllib2.urlopen(addr).read()
f = open(artName.decode("utf-8")+str(y)+'.jpg', 'wb')
f.write(data)
f.close() #具体帖子页面解析,得到图片链接地址,并使用loadImg下载 artName为帖子名
def getImgLink(html,x,artName):
relink = '<img src=".*" file="(.*)" width=".*" id=".*" alt=".*.jpg" />'
cinfo = re.findall(relink,html)
y = 0
for lin in cinfo:
imgAddr = 'http://www.xxx.com/'+lin
print "LoadImg:"+str(x),imgAddr+'\n'
t = threading.Thread(target=loadImg(imgAddr,x,y,artName)) #使用threading 多线程下载
t.start()
y = y+1 #论坛版块页面解析,得到具体帖子链接
def getArticleLink(html,page):
relink = '<a href="(viewthread\.php\?tid=.*3D.*)">(.*)</a>'
cinfo = re.findall(relink,html)
x = 1
for lin in cinfo:
#print lin,'\n'
url="http://www.xxx.com/"+lin[0]
headers={"User-Agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1"}
req = urllib2.Request(url,headers=headers)
response= urllib2.urlopen(req)
html = response.read()
getImgLink(html,x,lin[1])
x = x+1 start = 1 #起始页
end = 100 #终止页
for page in range(end):
url="http://www.xxx.com/forumdisplay.php?fid=19&page="+str(page+start)
headers={"User-Agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1"}
req = urllib2.Request(url,headers=headers)
response= urllib2.urlopen(req)
html = response.read()
print'Start'
getArticleLink(html,page)