import urllib.request
from lxml import etree
headers = ('Referer','http://bbs.tianya.cn/post-funinfo-2325132-1.shtml')#防盗链,修改访问来源
opener = urllib.request.build_opener()
opener.addheaders = [headers]
urllib.request.install_opener(opener)
for i in range(1,22):
url="http://bbs.tianya.cn/post-funinfo-2325132-"+str(i)+".shtml"
response = opener.open(url).read().decode("utf-8","ignore")
html = etree.HTML(response)
imagelist = html.xpath('//img/@original')
for j in range(0,len(imagelist)):
thisimg=imagelist[j]
thisimgurl=thisimg
file="D://python/tyimg/"+str(i)+str(j)+".jpg"
urllib.request.urlretrieve(thisimgurl,filename=file)