requests
```#进行爬取
import requests
import os
from requests_html import HTMLSession#必须使用session = HTMLSession()
session = HTMLSession()
os.makedirs('C:/表情包',exist_ok=True)
path='C:/表情包/'
a=0
fail=0
def save(respone,name):with open(path+name+'.jpg','wb') as f:f.write(respone)
def savegif(respone,name):with open(path + name + '.gif', 'wb') as f:f.write(respone)
def src(i):r=session.get('/'+str(i)+'.html')for i in range(1,46):div=r.html.find('#bqb > div.ui.segment.imghover > div:nth-child('+str(i)+') > a > img',first=True)# print(div.find('img'))#直接定位到img标签,具体分析,获取相应的数据try:print(div.attrs['data-original'])#获取到地址print(div.attrs['title'])#获取到titletitle=div.attrs['title']link=str(div.attrs['data-original'])print(link)connet=requests.get(link)if (link[-3:]=='jpg'):save(connet.content,title)else:savegif(connet.content, title)# with open(path + title + '.jpg', 'wb') as f:# f.write(connet.content)except:print("没有定位到超链接")global failfail=fail+1global aa=a+1print('在下载第d%张'%a)#下载了多少个print('失败d%张'%fail)for i in range(0,201):src(i)
发布者:admin,转转请注明出处:http://www.yc00.com/news/1704549512a1357191.html
评论列表(0条)