Py之Crawler:利用python的爬虫功能实现从各种网站上(以百度贴吧为例)获得你喜欢的照片下载到本地电脑上——Jason niu

import urllib.request
import re import os def open_url(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/56.0.2924.90 Safari/537.36 2345Explorer/9.2.1.17116") page=urllib.request.urlopen(req) html=page.read().decode("utf-8") return html def get_img(html): p=r'<img class="BDE_Image" src="([^"]+\.jpg)"' imglist=re.findall(p,html) for each in imglist: print(each) for each in imglist: filename=each.split("/")[-1] urllib.request.urlretrieve(each, filename,None) if __name__=="__mian__": url="http://tieba.baidu.com/p/3563409202" get_img(open_url(url)) url="http://tieba.baidu.com/p/3563409202" get_img(open_url(url))

 

posted @ 2018-03-16 21:14  一个处女座的程序猿  阅读(234)  评论(0编辑  收藏  举报