斗图?拥有几个G表情的我怕过谁
2019-01-14 本文已影响0人
数云智连
斗图?你可想清楚了,我可是有海量表情的人。先放一组表情包镇楼:





斗图越来越火, 俨然已经成为了一种文化。是表情包,令尬聊现场起死回生;也是表情包,令我们咸鱼日常的丧文化和丧表演有了灵魂。如果你手机里不存上几个G的表情包,加完好友根本不好意思跟人打招呼。
可是上哪找去?别怕,网上那么多斗图网站,上爬虫去抓点就有了。这才运行了10多分钟,爬了几千张了。


爬虫的实现也不复杂,以www.doutula.com为例:
1、循环下一页打开网站
def get_page(url):
resp = requests.get(url,headers=headers)
print(resp,url) #打印:是否访问成功、明细页
html = etree.HTML(resp.text)
page_link_deils = html.xpath('//div[@class="col-sm-9"]/a/@href')
for page_link_deil in page_link_deils:
[srcs,foldname] = get_pagelinkdeil(page_link_deil)
srcs_new = []
for src in srcs:
if src.endswith('!dta'):
src_new = src.split('!dta')[0]
srcs_new.append(src_new)
else:
srcs_new.append(src)
#多线程下载图片
ex = futures.ThreadPoolExecutor(max_workers=50)
for src in srcs_new:
ex.submit(download_img,src,foldname)
next_link = html.xpath('//li/a[@rel="next"]/@href')
return next_link
2、循环打开每页中的组图详情页链接
def get_pagelinkdeil(page_link_deil):
time.sleep(random.randint(1,4))
resp = requests.get(page_link_deil,headers=headers)
html = etree.HTML(resp.text)
srcs = html.xpath('//tr/td/a/img/@src')
foldname = "E:\\doutula\\{}{}".format(html.xpath('//div[@class="pic-title"]/h1/a/text()')[0],page_link_deil.split('/')[-1])
print(resp,page_link_deil,foldname) #打印:是否访问成功、明细页、文件路径
return srcs,foldname
3、循环获取详情页中的各表情图,下载到本地对于的表情包文件夹
def download_img(src,foldname):
filename = src.split('/')[-1]
if not os.path.exists(foldname):
os.makedirs(foldname)
img = requests.get(src,headers=headers)
with open('{}\\{}'.format(foldname,filename),'wb') as file:
file.write(img.content)
实现的python程序、下载的表情包,在个人同名微信工作号“数云智连”,有需要的到微信公众号回复:斗图表情包,获取下载链接。