[Python/爬虫]斗图网
#!/usr/bin/env python# *-* coding:utf-8 -*-from bs4 import BeautifulSoupimport reimport requestsurl = 'https://www.doutula.com/article/list/?page={}'class DownLoadImg(object):def __init__(self
·
#!/usr/bin/env python
# *-* coding:utf-8 -*-
from bs4 import BeautifulSoup
import re
import requests
url = 'https://www.doutula.com/article/list/?page={}'
class DownLoadImg(object):
def __init__(self,url):
self.url = url
self.img_url = 'lazy image_dtb img-responsive'
self.head = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'}
self.content = requests.get(self.url,headers=self.head)
self.content.encoding = 'utf-8'
self.GetTitle(self.content.content)
def GetTitle(self,uri):
url = uri
soup = BeautifulSoup(url,'html.parser')
info = soup.find_all('img',class_=self.img_url)
n = 0
for i in info:
url = requests.get('http:' + i['data-original']).content
if i.get('data-original').endswith('.jpg'):
f = open(r'C:\Users\Administrator\Desktop\Python\爬虫\斗图网图片\\%s%s.%s' % (i['alt'],n,'jpg'),'wb')
f.write(url)
f.close()
elif i.get('data-original').endswith('.gif'):
f = open(r'C:\Users\Administrator\Desktop\Python\爬虫\斗图网图片\\%s%s.%s' % (i['alt'],n,'gif'),'wb')
f.write(url)
f.close()
n += 1
if __name__ == '__main__':
for i in range(0,10):
print('开始下载第{}页图片'.format(i))
picture = DownLoadImg(url.format(i))
更多推荐
已为社区贡献12条内容
所有评论(0)