|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下(用的是www.mzitu.com这个网站)
- import urllib.request
- import os
- def url_open(url):
- req=urllib.request.Request(url)
- req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5221.400 QQBrowser/10.0.1125.400')
-
- response=urllib.request.urlopen(req)
- html=response.read()
- print(url)
- return html
- def get_page(url):
- html=url_open(url).decode('utf-8')
- a=html.find('page-numbers current')+22
- b=html.find('<',a)
-
- return html[a:b]
- def find_imgs(url):
- html=url_open(url).decode('utf-8')
- img_addrs=[]
- a=html.find('img class=''lazy'' src=')
- while a!=-1:
- b=html.find('.jpg',a,a+255)
- if b!=-1:
- img_addrs.append('http:'+html[a+22:b+4])
- else:
- b=a+22
- a=html.find('img class=''lazy'' src=',b)
- for each in img_addrs:
- print(each)
- return img_addrs
- def save_imgs(folder,img_addrs):
- for each in img_addrs:
- filename=each.splt('/')[-1]
- with open(filename,'wb') as f:
- img=open_url(each)
- f.write(img)
-
- def download_mm(folder='ooxx',pages=10):
- os.mkdir(folder)
- os.chdir(folder)
- url='http://www.mzitu.com/zipai/'
- page_num=int(get_page(url))
- for i in range(pages):
- page_num-=i
- page_url=url+'comment-page-'+str(page_num)+'/#comments'
- img_addrs=find_imgs(page_url)
- save_imgs(folder,img_addrs)
- if __name__=='__main__':
- download_mm()
复制代码
没有报错,运行结果如下,但是生成的文件夹是空的:
http://www.mzitu.com/zipai/
http://www.mzitu.com/zipai/comment-page-455/#comments
http://www.mzitu.com/zipai/comment-page-454/#comments
http://www.mzitu.com/zipai/comment-page-452/#comments
http://www.mzitu.com/zipai/comment-page-449/#comments
http://www.mzitu.com/zipai/comment-page-445/#comments
http://www.mzitu.com/zipai/comment-page-440/#comments
http://www.mzitu.com/zipai/comment-page-434/#comments
http://www.mzitu.com/zipai/comment-page-427/#comments
http://www.mzitu.com/zipai/comment-page-419/#comments
http://www.mzitu.com/zipai/comment-page-410/#comments |
|