import requests
from lxml import etree
import os
def ck_dir():
if not os.path.exists('pics'):
os.mkdir('pics')
def main():
# num = int(input('请输入下载的页数:'))
num = 2
count = 1
global url
n_url = url
ck_dir()
for i in range(num):
r = requests.get(n_url, headers=headers)
html = etree.HTML(r.text)
pics_list = html.xpath('//a[@class="view_img_link"]/@href')
for item in pics_list:
p_url = 'http:' + item
picname = item.split('/')[-1]
r = requests.get(p_url, headers=headers)
with open(picname, 'wb') as f:
f.write(r.content)
print('已下载%s, 共下载%d张。' % (picname, count))
count += 1
n_url = 'http:' + html.xpath('//a[@class="previous-comment-page"]/@href')[0]
if __name__ == '__main__':
url = 'http://jandan.net/ooxx'
headers = {'user-agent': 'firefox'}
main()
|