Python网络爬虫爬取站长素材上的表情包
来源:互联网 发布:关口知宏五台山 编辑:程序博客网 时间:2024/04/29 21:12
由于不经常看群消息,收藏的表情包比较少,每次在群里斗图我都处于下风,最近在中国大学MOOC上学习了嵩天老师的Python网络爬虫与信息提取课程,于是决定写一个爬取网上表情包的网络爬虫。通过搜索发现站长素材上的表情包很是丰富,一共有446页,每页10个表情包,一共是4000多个表情包,近万个表情,我看以后谁还敢给我斗图
技术路线
requests+beautifulsoup
网页分析
站长素材第一页表情包是这样的:
可以看到第一页的url是:http://sc.chinaz.com/biaoqing/index.html
点击下方的翻页按钮可以看到第二页的url是:http://sc.chinaz.com/biaoqing/index_2.html
可以推测第446页的url是:http://sc.chinaz.com/biaoqing/index_446.html
接下来是分析每一页表情包列表的源代码:
再来分析每个表清包全部表情对应的网页:
步骤
1、获得每页展示的每个表情包连接和title
2、获得每个表情包的所有表情的链接
3、使用获取到的表情链接获取表情,每个表情包的表情放到一个单独的文件夹中,文件夹的名字是title属性值
代码
#-*-coding:utf-8-*-'''Created on 2017年3月18日@author: lavi'''import bs4from bs4 import BeautifulSoupimport reimport requestsimport osimport traceback'''获得页面内容'''def getHtmlText(url): try: r = requests.get(url,timeout=30) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: return ""'''获得content''' def getImgContent(url): head = {"user-agent":"Mozilla/5.0"} try: r = requests.get(url,headers=head,timeout=30) print("status_code:"+r.status_code) r.raise_for_status() return r.content except: return None '''获得页面中的表情的链接''' def getTypeUrlList(html,typeUrlList): soup = BeautifulSoup(html,'html.parser') divs = soup.find_all("div", attrs={"class":"up"}) for div in divs: a = div.find("div", attrs={"class":"num_1"}).find("a") title = a.attrs["title"] typeUrl = a.attrs["href"] typeUrlList.append((title,typeUrl))def getImgUrlList(typeUrlList,imgUrlDict): for tuple in typeUrlList: title = tuple[0] url = tuple[1] title_imgUrlList=[] html = getHtmlText(url) soup = BeautifulSoup(html,"html.parser") #print(soup.prettify()) div = soup.find("div", attrs={"class":"img_text"}) #print(type(div)) imgDiv = div.next_sibling.next_sibling #print(type(imgDiv)) imgs = imgDiv.find_all("img"); for img in imgs: src = img.attrs["src"] title_imgUrlList.append(src) imgUrlDict[title] = title_imgUrlListdef getImage(imgUrlDict,file_path): head = {"user-agent":"Mozilla/5.0"} countdir = 0 for title,imgUrlList in imgUrlDict.items(): #print(title+":"+str(imgUrlList)) try: dir = file_path+title if not os.path.exists(dir): os.mkdir(dir) countfile = 0 for imgUrl in imgUrlList: path = dir+"/"+imgUrl.split("/")[-1] #print(path) #print(imgUrl) if not os.path.exists(path): r = requests.get(imgUrl,headers=head,timeout=30) r.raise_for_status() with open(path,"wb") as f: f.write(r.content) f.close() countfile = countfile+1 print("当前进度文件夹进度{:.2f}%".format(countfile*100/len(imgUrlList))) countdir = countdir + 1 print("文件夹进度{:.2f}%".format(countdir*100/len(imgUrlDict))) except: print(traceback.print_exc()) #print("from getImage 爬取失败") def main(): #害怕磁盘爆满就不获取全部的表情了,只获取30页,大约300个表情包里的表情 pages = 30 root = "http://sc.chinaz.com/biaoqing/" url = "http://sc.chinaz.com/biaoqing/index.html" file_path = "e://biaoqing/" imgUrlDict = {} typeUrlList = [] html = getHtmlText(url); getTypeUrlList(html,typeUrlList) getImgUrlList(typeUrlList,imgUrlDict) getImage(imgUrlDict,file_path) for page in range(pages): url = root + "index_"+str(page)+".html" imgUrlDict = {} typeUrlList = [] html = getHtmlText(url); getTypeUrlList(html,typeUrlList) getImgUrlList(typeUrlList,imgUrlDict) getImage(imgUrlDict,file_path) main()结果
如果你在群里斗图吃了亏,把上面的程序运行一遍。。。不要谢我,3月是学雷锋月。哈哈,来把我们斗会图,
1 0
- Python网络爬虫爬取站长素材上的表情包
- Python爬取网站gif表情包
- python使用多线程爬取表情包
- [Python]网络爬虫学习笔记,爬取豆瓣妹子上妹子的照片
- python写网络爬虫:爬取糗事百科上的段子
- Python爬虫(二)“我想要很多很多的表情包”
- Python可自动登录爬取图片的网络爬虫
- python爬虫实现网络股票信息爬取的demo
- 老司机用Python多线程爬取表情包
- python网络爬虫爬取图片代码
- python爬虫(19)爬取论坛网站——网络上常见的gif动态图
- python网络爬虫学习(二)一个爬取百度贴吧的爬虫程序
- 【Python网络爬虫学习01】爬取网络验证码
- Python 网络爬虫 005 (编程) 如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫
- 【爬虫】爬取煎蛋上的段子
- 爬虫-爬取网站上的图片
- python爬虫爬取网上的照片
- python爬虫----简单的图片爬取
- 给IT新人的15个建议:程序员的辛酸反省与总结!
- Activity管理类,随时随地退出应用程序
- linux gz 解压缩
- Linux的权限管理
- c++的图的邻接表表示方法实现
- Python网络爬虫爬取站长素材上的表情包
- 山头阻击战----二分法的运用
- ACM程序设计书中题目--L(字符逆序输出)
- 软考学习笔记
- 常见外设电路图
- HTML5按比例缩略图片并上传的实例
- 24种设计模式——访问者模式
- MySQL删除同一个字段的多条记录
- 数组的定义和基本操作