python3学习爬虫 正则以及url
来源:互联网 发布:update数据库 编辑:程序博客网 时间:2024/05/12 18:04
#coding=utf8__author__ = 'Administrator'import osimport reimport urllib.requestimport pymysqlclass Spider: #页面初始化 def __init__(self,url,retext,path): self.url = url self.path = path self.retext = retext def mkdir(self,path): isExists = os.path.exists(path) if not isExists: os.makedirs(path) return path def getData(self): url = urllib.request.Request(self.url) html = urllib.request.urlopen(url).read() print(html) html = html.decode('utf-8','ignore') imgRe = re.compile(self.retext) data = imgRe.findall(html) return data #self.data = data def saveImg(self,imgurl,imgname): #img = urllib.request.urlopen(imgurl).read() #img = urllib.request.urlopen(imgurl) #print(img) path = self.path try: img = urllib.request.urlopen(imgurl) #print(img) except Exception as e: print(e) else: img = img.read() f = open("./%s/%s.jpg" %(path,imgname),'wb') f.write(img) f.close()
def saveMysql(self,title,url,catogary,content): try: con=pymysql.connect(host='qdm***w.com',user='q****46',passwd='*******',db='qd*****db',port=3306,charset='utf8') cur=con.cursor() #cur.execute('select * from imgurl') #data=cur.fetchall() insert = "insert into pic(title,url,catogary,content) values ('%s','%s','%s')"%(title,url,catogary,content) #print(insert) cur.execute(insert) cur.close()#关闭游标 con.close()#释放数据库资源 except Exception as e: print("发生异常:%s"%e)
def getContent(self): path = self.mkdir(self.path) data = self.getData() #print(data) fp = open('./%s/url.txt'%(path),'w+') x = 0 for d in data: print(d) fp.write(d) if (len(d) < 80) : #self.saveImg(d,x) #print(d) x+=1 fp.close()url = "http://www.zhihu.com/question/29649162"url2 = 'http://image.baidu.com/activity/starfans/4093640704%201415350495?&albumtype=1'retext = r'http://.*?\.jpg|http://.*?\.png'retext2 = r'<h2 class="zm-item-title.*?>(.*?)</h2>'spider = Spider(url,retext2,"赵丽颖")spider.getContent()
0 0
- python3学习爬虫 正则以及url
- Python3爬虫学习笔记(3.正则详解及实例)
- python3爬虫学习
- Python3简单爬虫学习
- python3爬虫基础学习
- Python3爬虫入门学习
- python3爬虫学习
- python爬虫URL重试机制实现(python2.7以及python3.5)
- python3.x之爬虫学习
- python3 爬虫学习日记【一】
- python3爬虫学习(一)
- Python3爬虫视频学习教程
- python3爬虫之入门和正则表达式
- Python3爬虫之入门和正则表达式
- Python3爬虫之入门和正则表达式
- python3爬虫之入门和正则表达式
- [Python3]爬虫入门之正则表达式
- python3爬虫初级入门和正则表达式
- poj 1178 Camelot 枚举
- 第十周项目2-二叉树遍历的递归算法
- web服务器并发优化(nginx+多实例tomcat负载均衡)(另附:安装包和文档)
- 什么是C++虚函数、虚函数的作用和使用方法
- 轻松学习JavaScript三:JavaScript与HTML的结合
- python3学习爬虫 正则以及url
- 第十二周项目1 图基本算法库
- 第十四周项目2-二叉树排序树中查找的路径
- USING THE ANDROID TOOLCHAIN AS A STANDALONE COMPILER
- OpenCV 如何保存图片
- DB2新建实例参数
- 第十二周 利用遍历思想求解图问题(经过顶点的所有简单路径)
- 查看凭证更改记录的三种方式
- 第十四周实践项目1--验证折半查找